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.
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.
city = "Lisbon"
year = 2026
print(city)
print(year)
print("I live in", city)Lisbon 2026 I live in Lisbon
= 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.
score = 10
print(score)
score = 15
print(score)
score = score + 5
print(score)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.
a = 5
b = a
a = 8
print(a)
print(b)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.
name = "Omar"
age = 29
height = 1.82
likes_tea = True
print(type(name))
print(type(age))
print(type(height))
print(type(likes_tea))<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Naming rules
- Names can contain letters, digits and the underscore
_, but cannot start with a digit:price2works,2pricedoes not. - No spaces: write
first_name, notfirst name. Python programmers join words with_(this style is called snake_case). - Upper and lower case are different:
nameandNameare two separate variables. - You cannot use Python's keywords such as
if,fororTrueas names. Also avoid the names of built-in functions such asprint: Python allows it, but the function then stops working in your program. - Pick names that say what is inside:
total_priceis better thantporx.
✎ 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.
pet = "cat"
# your code here
Expected output:
My pet is a cat Its name is Milo Now its name is Luna
Show a solution
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)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?
Python calculates x + 2 with the current value (3 + 2) and stores the result back in x.
2. Which of these is a valid variable name?
Names cannot start with a digit or contain spaces. second_place follows the rules.
3. What is the type of 4.0?
Any number written with a decimal point is a float, even when the part after the point is zero.