Variables act as “storage locations” for data in a program. They are a way of naming information for later usage.
Each variable has a name; an example variable name we will use is myLuckyNumber. To store information in a variable, we write a command using an equal sign in the following way:
«the variable name» = «the value you want to store»(We use «double angle brackets» in our lessons, like above, to indicate special parts of expressions.)
For example, the Python line
myLuckyNumber = 13stores the value
13 in the variable myLuckyNumber. Then, anywhere you write the variable name myLuckyNumber again, Python retrieves the stored value.
Below, there is a short example of using variables. It has more than one line of instructions: Python executes the first line, then the second line, and so forth until it reaches the last line. Press the Run program button to see what it does.
Look at the 5 lines of the program in order, and how they correspond to the output. As you can see, keeps its value of myLuckyNumber13 for the first two print statements, then its value is changed to 7.
We also introduced the plus operator (+) above, which adds two numbers together. Similarly, there are operators for subtraction (-), multiplication (*), and division (/). We'll return to these in a later lesson.
You can simulate the memory storage of a computer with paper and pencil, by keeping track of the values in a table. Here is an example; remember that * means multiplication in Python.
Goal: determine the final values of all variables at the end of the program.
first = 2 second = 3 third = first * second second = third - first first = first + second + third third = second * firstIdea: We use a table to keep track of the values as they change. Scroll to the bottom to see the final answer.
| Statement | Values after statement executes | ||
|---|---|---|---|
| first | second | third | |
| first = 2 | 2 | ||
| second = 3 | 2 | 3 | |
| third = first * second | 2 | 3 | 6 |
| second = third - first | 2 | 3 4 | 6 |
| first = first + second + third | 2 12 | 4 | 6 |
| third = second * first | 12 | 4 | 6 48 |
Thus at the end of the program, the value of first is 12, the value of second is 4, and the value of third is 48.
![]() | Try the Python3 visualization tool to virtually execute programs like this one step at a time. We recommend it as a useful resource whenever you are confused about what Python is doing, or when you are stuck debugging! You can find this link again later in the Resources page. |
Here is a short answer exercise about variables.
x Marks the Spotx after these commands execute?
x = 10
x = x + x
x = x - 5
Undefined Variables
If you ask Python about a variable that has not been defined, you get an error.
As you can see, we get an error message saying NameError: name 'trouble' is not defined. Sometimes you can get errors like this from simple typos: if you define a variable address=32, then try to print(adress), the same type of error occurs.
Exercise
This is a warm-up to get you started with variables.
Code Scramble
The next item in this lesson is a new type of programming exercise, where you don't have to do any programming. We will provide you with a correct program, but the catch is that its lines have been put into a scrambled-up order. Your job is to drag-and-drop the lines to rearrange them into a correct program.
Switcheroo
Here is the last exercise in this lesson.
Once you get this exercise correct, you are ready to move on to the next lesson. Click the Next button below.


