PY-101 ยท Variables and data types
Variables
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
Lesson quest ยท 4 min
Help a shop app remember the cart total while it keeps changing.
- Watch it run
- Predict
- Drag and sort
- Predict
- Fill the gaps
- Watch it run
- Build it
- Boss level
- Badge: Box Keeper
Watch it run
Watch the cart remember
Tap Run line and watch the box.
cart = 0cart = cart + 250cart = cart + 40print(cart)
๐ง Memory
Empty. No boxes yet.
๐ฅ๏ธ Output
Press Run line to start.
Predict
What does this print?
age = 25
print(age) Drag and sort
Label or not a label?
Drag each name into the right bin.
Predict
What does this print?
The right side of = runs first.
x = 4
x = x + 3
print(x) Fill the gaps
Make a box and show it
city "Chennai" (city)
Watch it run
The swap trick
Two boxes trade values in one line.
a, b = 1, 2a, b = b, aprint(a, b)
๐ง Memory
Empty. No boxes yet.
๐ฅ๏ธ Output
Press Run line to start.
Build it
Build the program
Make it print: Alex is 26
Boss level
Boss level: the checkout
Put 100 in a box called price, add 50, then print it. Expected output: 150
๐ก Hint
price = 100, then price = price + 50, then print(price)โก The 30-second answer
A variable is a name that points to a value, such as age = 25. You create it by assigning with =, and you can assign a new value at any time. Python works out the type from the value, so you never declare it.
- Create variables with =
- Change a variable's value
- Follow Python's naming rules
- Assign several variables in one line
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
name = "Alex"age = 25age = age + 1city, country = "Chennai", "India"print(name, "is", age)print(city, country)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Alex is 26
Chennai India ๐ฏ Swap the values of a and b so that the program prints 9 5. Do not type the numbers again; use the variables.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
Python can do it in one line: a, b = b, a
- The problemA shopping cart shows โน0. You add a โน250 book and it shows โน250. Add a pen and it changes again. Where does the app keep that number while it keeps changing?
- The Python idea
name = valueA variable is a name that points to a value, such as age = 25. - In real lifeIn a shopping app, cart_total starts at 0 and changes every time an item is added. That running total is just a variable being reassigned.
Start with a question
A shopping cart shows โน0. You add a โน250 book and it shows โน250. Add a pen and it changes again. Where does the app keep that number while it keeps changing?
Every program stores data: a user's name, a cart total, a model's score. Variables are how you give that data a name you can use later.
Explain it like I'm 10
A variable is a labelled box. age = 25 means: put 25 in the box labelled age. Later you can look inside the box, or swap what is in it.
The technical explanation
= means assign, not "equals". age = 25 means "make the name age point to 25".
The right side runs first. In age = age + 1, Python reads the current age, adds 1, then points age at the new value.
Names can contain letters, digits and underscores, cannot start with a digit, and are case-sensitive: Age and age are different. Python style is lower case with underscores, called snake_case: total_price.
You cannot use keywords such as if, for or class as names.
| Name | Valid? | Why |
|---|---|---|
total_price | Yes | snake_case, the Python style |
_count | Yes | Can start with an underscore |
2nd_place | No | Cannot start with a digit |
first-name | No | Hyphen means minus |
class | No | class is a keyword |
Syntax
name = value
a, b = 1, 2 Real-world scenario
In a shopping app, cart_total starts at 0 and changes every time an item is added. That running total is just a variable being reassigned.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Data types โQuestions people ask
Do you need to declare variables in Python?
No. A variable exists the moment you assign to it. Python works out the type from the value.
Can a Python variable change type?
Yes. x = 5 then x = "five" is allowed, because the name just points at a new value. It works, but it makes code harder to follow, so avoid it.
What is snake_case?
Lower-case words joined by underscores, such as total_price. It is the naming style recommended for Python variables and functions in PEP 8.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Do you need to declare variables in Python?
No. A variable exists the moment you assign to it. Python works out the type from the value.
beginner Can a Python variable change type?
Yes. x = 5 then x = "five" is allowed, because the name just points at a new value. It works, but it makes code harder to follow, so avoid it.
beginner What is snake_case?
Lower-case words joined by underscores, such as total_price. It is the naming style recommended for Python variables and functions in PEP 8.
intermediate What does = do in Python?
Assigns a value to a name. = assigns. == is the equality check.
intermediate Which variable name is valid?
total_price. total_price uses letters and an underscore. Names cannot start with a digit, contain a hyphen or be a keyword.
Quick revision
Remember
- Create variables with =
- Change a variable's value
- Follow Python's naming rules
- Assign several variables in one line
- Python works out the right side of = first. That is why
count = count + 1works: it uses the old count to make the new one.
Syntax
name = value
a, b = 1, 2 Official documentation
- Tutorial: an informal introduction (docs.python.org)
- PEP 8 naming conventions (docs.python.org)