Beginner 8 min

Variables and values

Give values names and follow what changes when you reassign a Python variable.

A name points to a value

In Python, assignment binds a name to an object. In steps = 2, the name steps refers to the integer 2. The name can later refer to another value. You can inspect a value with print and ask about its type with type.

How it works

Draw a name on the left and the value it points to on the right. After steps = 2, write steps → 2. On previous = steps, add another arrow to 2. The update steps = steps + 3 moves only the steps arrow to 5. The output is 2 followed by 5.

Read the right side first

On steps = steps + 3, Python reads the current value, performs the addition, and then binds the result to steps. This is an update, even though it looks like an equation. Two names can refer to the same object, and changing one name’s binding leaves the other binding intact.

Spot a type mistake

The integer 3 and the string "3" represent different things. Adding two integers performs arithmetic. Adding two strings joins their text. Adding an integer to a string raises a TypeError. Convert explicitly when the conversion makes sense for the data.

Code example

steps = 2
previous = steps
steps = steps + 3
print(previous)
print(steps)

Check your understanding

Add 4 to the starting number and print the result. Try replacing the starting value for each case.

See the example and explanation
Example 1
Given 2
Answer 6
Example 2
Given 0
Answer 4

Use steps = steps + 4 and then print(steps). A starting value of 2 becomes 6 and zero becomes 4.

Open practice problem
Next lessonNumbers, operators, and conversion