Python for beginners · Lesson 2 of 15

Variables: give your data a name

Store values in variables, change them, learn the basic types (str, int, float, bool) and the rules for good names.

About 12 minutes · code runs in your browser, nothing to install

A variable is a name that points to a value, like a label on a box. You create one with =: the name goes on the left, the value on the right. After that, you can use the name anywhere you would use the value.

Pythoneditable
city = "Lisbon"
year = 2026
print(city)
print(year)
print("I live in", city)
Output
Lisbon
2026
I live in Lisbon
Note: In Python, = does not mean "is equal to" like in maths. It means "store the value on the right under the name on the left". Read year = 2026 as "year becomes 2026".

Changing a variable

A variable can get a new value at any time. The old value is forgotten. You can even use the variable's current value to calculate its new one.

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

score + 5 is calculated first (15 + 5), then the result goes back into score.

When you copy one variable into another, the second one gets the value as it is at that moment. Changing the first one later does not change the copy.

Pythoneditable
a = 5
b = a
a = 8
print(a)
print(b)
Output
8
5

Types of values

Every value has a type. The four you will meet first are: str (text, a string), int (a whole number), float (a number with a decimal point) and bool (True or False). The type() function tells you the type of any value.

Pythoneditable
name = "Omar"
age = 29
height = 1.82
likes_tea = True
print(type(name))
print(type(age))
print(type(height))
print(type(likes_tea))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

Naming rules

  • Names can contain letters, digits and the underscore _, but cannot start with a digit: price2 works, 2price does not.
  • No spaces: write first_name, not first name. Python programmers join words with _ (this style is called snake_case).
  • Upper and lower case are different: name and Name are two separate variables.
  • You cannot use Python's keywords such as if, for or True as names. Also avoid the names of built-in functions such as print: Python allows it, but the function then stops working in your program.
  • Pick names that say what is inside: total_price is better than tp or x.

✎ Exercise

Create a variable pet with the value "cat" and a variable pet_name with the value "Milo". Print them as My pet is a cat and Its name is Milo, using the variables. Then change pet_name to "Luna" and print Now its name is Luna.

Your codewrite your answer, then Run
pet = "cat"
# your code here

Expected output:

My pet is a cat
Its name is Milo
Now its name is Luna
Show a solution
Solutioneditable
pet = "cat"
pet_name = "Milo"
print("My pet is a", pet)
print("Its name is", pet_name)
pet_name = "Luna"
print("Now its name is", pet_name)
Output
My pet is a cat
Its name is Milo
Now its name is Luna

Check yourself

1. After x = 3 and then x = x + 2, what is x?

2. Which of these is a valid variable name?

3. What is the type of 4.0?

Spotted a mistake on this page? Tell us