Python course menu
Python Variables: How to Store and Use Values
Learn Python variables from scratch: how to create and update them, naming rules, multiple assignment, f-strings, and the beginner mistakes to avoid. Includes exercises.
On this page
Programs need to remember things: a user's name, a score, the price of an item. A variable is a name that refers to a value, so you can use and change that value later.
Creating a variable#
You create a variable by writing a name, an equals sign, and a value:
city = "Mumbai"
temperature = 31
print(city)
print(temperature)Mumbai
31The = here is the assignment operator. Read temperature = 31 as "temperature now refers to 31", not as "temperature equals 31" in the maths sense. Python takes the value on the right and attaches the name on the left to it.
Using variables in calculations#
Once a value has a name, you can use the name anywhere you would use the value:
price = 250
quantity = 4
total = price * quantity
print(total)1000Changing a variable#
Assigning again replaces the old value:
score = 10
print(score)
score = 15
print(score)
score = score + 5
print(score)10
15
20score = score + 5 looks strange at first. Python always works out the right side first (current score 15 plus 5 is 20), then stores the result back in score.
There is a shorter way to write this, called an augmented assignment:
score = 20
score += 5 # same as score = score + 5
score -= 3 # same as score = score - 3
score *= 2 # same as score = score * 2
print(score)44Variables can change type#
A variable is just a label, so the same name can point to a different kind of value later:
answer = 42
answer = "forty-two"
print(answer)forty-twoThis is allowed, but doing it on purpose usually makes code confusing. Keep each variable holding one kind of value.
Naming rules#
Python enforces a few rules:
- Names can contain letters, digits, and underscores (
_). - Names cannot start with a digit:
2nd_placeis invalid,second_placeis fine. - Names cannot contain spaces or hyphens:
user nameanduser-nameare invalid. - Names are case-sensitive:
ageandAgeare different variables. - You cannot use Python's reserved words such as
if,for,class,True, orNone.
Naming conventions#
Beyond the rules, Python programmers follow a style guide called PEP 8. For variables it recommends snake_case: all lowercase, with words separated by underscores.
Assigning several variables at once#
x, y, z = 1, 2, 3
print(x, y, z)
a = b = 0
print(a, b)1 2 3
0 0This also gives Python a famously neat way to swap two values:
first = "left"
second = "right"
first, second = second, first
print(first, second)right leftPutting variables into text with f-strings#
The easiest way to mix variables into text is an f-string. Put an f before the opening quote and wrap variable names in curly braces:
name = "Priya"
items = 3
total = 749.5
print(f"{name} bought {items} items for ₹{total}")
print(f"Average price: ₹{total / items:.2f}")Priya bought 3 items for ₹749.5
Average price: ₹249.83You can put any expression inside the braces. The :.2f part formats the number with exactly two decimal places.
Common mistakes#
Practice#
Practice exercise
A shop sells notebooks at ₹45 each. Write a program that:
- Stores the price and a quantity of 6 in two variables.
- Calculates the total.
- Applies a 10% discount.
- Prints:
6 notebooks cost ₹243.00 after discount
Show hint
The discounted total is total * 0.9. Use an f-string with :.2f to show two decimal places.
Show solution
price = 45
quantity = 6
total = price * quantity
discounted_total = total * 0.9
print(f"{quantity} notebooks cost ₹{discounted_total:.2f} after discount")6 notebooks cost ₹243.00 after discount45 * 6 is 270, and 90% of 270 is 243.0. The :.2f format shows it as 243.00.
Key takeaway#
Finished this lesson?