Skip to content
Theory · Variables and types · lesson 1 of 7

Variables

What you'll learn

What you'll learn:

  • create variables and change their values;
  • explain why b keeps its old value after b = a and a = 20;
  • tell the types int, float, str, bool and None apart and find a type with type();
  • turn a string into a number and a number into a string with int(), float() and str().
A tag price on a long string is tied to a token 250 and only points at it

Picture a luggage tag on a long string. The tag says price, and the string is tied to a round token with the number 250. The tag and the token are two separate objects: the tag does not hold the number inside, it only points at the token.

That is how variables work in Python. A name is the tag, and a value is the token it is tied to.

Why do you need names at all? A program often uses one value on several lines: the price of an item, a game score, a number of attempts. If you write the number itself on every line, changing the price means fixing all of them. Instead, you give the value a name and write the name from then on:

Python
price = 250
count = 3
print(price * count)  # 750
print(price, count)  # 250 3

Let's walk through the example line by line:

  1. price = 250 ties the tag price to the token 250.
  2. count = 3 ties the tag count to the token 3.
  3. print(price * count) finds both tokens by their tags, multiplies the numbers and prints 750.
  4. print(price, count) prints 250 3: the values the tags point at, not the names themselves.

If the price changes, fixing the first line is enough.

A name and an object

The line price = 250 is an assignment. Python evaluates the expression to the right of = and binds the name on the left to the result. Such a name is called a variable. You do not declare a variable in advance: it appears at its first assignment.

The value 250 itself is stored in memory as an object—that is the token. The variable only refers to the object. When print receives a name, it prints the object that name refers to.

The = sign is not equality from math but a command. The right side is evaluated first, and only then is the name bound to the result. So x = x + 1 makes sense: Python takes the current value of x, adds 1 to it and reties the tag x to the new object.

Rebinding

A new assignment reties a tag to a different object. The other tags stay where they are:

Python
a = 10
b = a
a = 20
print(a, b)  # 20 10

Let's go step by step:

  1. a = 10 ties the tag a to the object 10.
  2. b = a does not tie b to the name a. It ties a second tag, b, to the same object 10 that a points at right now.
  3. a = 20 reties only the tag a, to the new object 20.
  4. print(a, b) prints 20 10: the tag b still hangs on 10.

Press "Next step" and watch y: after the line x = 7 its value stays the same.

LiveTwo tags on one valuestep 1 of 7 · frames: 1

The tag x is tied to the object 5.

Watch y after the line x = 7

Interviewers like to ask this: "Does b change if you assign a new value to a after b = a?" The answer is no. An assignment reties only the tag whose name stands to the left of =.

This is where the tag analogy stops working: there is no tag without a token. A name appears only at the moment of assignment, together with its object. Before that, Python has no such tag.

The short forms += and -=

score += 5 is a short form of score = score + 5. The -= and *= forms work the same way:

Python
score = 0
score += 5
score -= 2
print(score)  # 3

Let's go step by step:

  1. score = 0 sets the starting value.
  2. score += 5 takes 0, adds 5 and reties score to 5.
  3. score -= 2 takes 5, subtracts 2, and now score points at 3.
  4. print(score) prints 3.

Variable names

Three rules apply to names in Python:

  • by PEP 8, names are written in lowercase letters, with words separated by underscores: item_count, max_speed;
  • a name does not start with a digit;
  • letter case matters: Score and score are two different variables.

Common mistake

+= is used on a variable that has not been assigned anything yet:

Python
total += 5  # ✗
print(total)
TEXT
NameError: name 'total' is not defined

total += 5 first reads the current value of total, and there is none: the tag total does not exist yet. The program stops at this line with a NameError. Before the first +=, assign the variable a starting value, such as total = 0.

Key points

  • A variable is a name tag that refers to an object; it appears at the first assignment.
  • = evaluates the right side first and then binds the name on the left to the result.
  • After b = a both tags hang on one object, and a = 20 reties only a.
  • x += 1 is a short form of x = x + 1, and x must already have a value before it.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way