Skip to content
Laviyx Learn

Try

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.

Beginner8 min readUpdated
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:

Python
city = "Mumbai"
temperature = 31

print(city)
print(temperature)
Output
Mumbai
31

The = 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:

Python
price = 250
quantity = 4
total = price * quantity

print(total)
Output
1000

Changing a variable#

Assigning again replaces the old value:

Python
score = 10
print(score)

score = 15
print(score)

score = score + 5
print(score)
Output
10
15
20

score = 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:

Python
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)
Output
44

Variables can change type#

A variable is just a label, so the same name can point to a different kind of value later:

Python
answer = 42
answer = "forty-two"
print(answer)
Output
forty-two

This 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_place is invalid, second_place is fine.
  • Names cannot contain spaces or hyphens: user name and user-name are invalid.
  • Names are case-sensitive: age and Age are different variables.
  • You cannot use Python's reserved words such as if, for, class, True, or None.

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.

GoodAvoidWhy
user_nameuserName, UserNamesnake_case is the Python convention
total_pricetp, xdescriptive names explain themselves
is_logged_inflaga name should say what the value means
MAX_RETRIES = 3max_retries = 3ALL_CAPS signals a value that should not change (a constant)

Assigning several variables at once#

Python
x, y, z = 1, 2, 3
print(x, y, z)

a = b = 0
print(a, b)
Output
1 2 3
0 0

This also gives Python a famously neat way to swap two values:

Python
first = "left"
second = "right"
first, second = second, first
print(first, second)
Output
right left

Putting 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:

Python
name = "Priya"
items = 3
total = 749.5

print(f"{name} bought {items} items for ₹{total}")
print(f"Average price: ₹{total / items:.2f}")
Output
Priya bought 3 items for ₹749.5
Average price: ₹249.83

You 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:

  1. Stores the price and a quantity of 6 in two variables.
  2. Calculates the total.
  3. Applies a 10% discount.
  4. 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
Python
price = 45
quantity = 6

total = price * quantity
discounted_total = total * 0.9

print(f"{quantity} notebooks cost ₹{discounted_total:.2f} after discount")
Output
6 notebooks cost ₹243.00 after discount

45 * 6 is 270, and 90% of 270 is 243.0. The :.2f format shows it as 243.00.

Key takeaway#

Finished this lesson?