When we program, we will often want to save the values that our expressions evaluate to so we can use them later in the program. We can store values in
variables
.Think of variables like a box that can hold values. You can store values inside variables with the = sign (called
the assignment operator
). For example, to store the value 15 in a variable named “spam”, enter spam = 15 into the shell:>>> spam = 15 >>>
Figure 2-4: Variables are like boxes that can hold values in them.
You can think of the variable like a box with the value 15 inside of it (as shown in Figure 2-4). The variable name “spam” is the label on the box (so we can tell one variable from another) and the value stored in it is like a small note inside the box.
When you press Enter you won't see anything in response, other than a blank line. Unless you see an error message, you can assume that the instruction has been executed successfully. The next >>> prompt will appear so that you can type in the next instruction.
This instruction (called an
assignment statement
) creates the variable spam and stores the value 15 in it. Unlike expressions,statements
are instructions that do not evaluate to any value, which is why there is no value displayed on the next line in the shell.It might be confusing to know which instructions are expressions and which are statements. Just remember that if the instruction evaluates to a single value, it's an expression. If the instruction does not, then it's a statement.
An assignment statement is written as a variable, followed by the = equal sign, followed by an expression. The value that the expression evaluates to is stored inside the variable. The value 15 by itself is an expression. Expressions made up of a single value by itself are easy to evaluate. These expressions just evaluate to the value itself. For example, the expression 15 evaluates to 15!
Remember, variables store values, not expressions. For example, if we had the statement, spam = 10 + 5, then the expression 10 + 5 would first be evaluated to 15 and then the value 15 would be stored in the variable, spam.
The first time you store a value inside a variable by using an assignment statement, Python will create that variable. Each time after that, an assignment statement only replaces the value stored in the variable.
Now let's see if we've created our variable properly. If we type spam into the shell by itself, we should see what value is stored inside the variable spam.
>>> spam = 15 >>> spam 15
>>>
Now, spam evaluates to the value inside the variable, 15.
And here's an interesting twist. If we now enter spam + 5 into the shell, we get the integer 20, like so.
>>> spam = 15 >>> spam + 5 20
>>>
That may seem odd but it makes sense when we remember that we set the value of spam to 15. Because we've set the value of the variable spam to 15, writing spam + 5 is like writing the expression 15 + 5.
If you try to use a variable before it has been created, Python will give you an error because no such variable would exist yet. This also happens if you mistype the name of the variable. We can change the value stored in a variable by entering another assignment statement. For example, try the following:
>>> spam = 15 >>> spam + 5 20 >>> spam = 3 >>> spam + 5 8 >>>
The first time we enter spam + 5, the expression evaluates to 20, because we stored the value 15 inside the variable spam. But when we enter spam = 3, the value 15 is replaced, or overwritten, with the value 3. Now, when we enter spam + 5, the expression evaluates to 8 because the value of spam is now 3.
To find out what the current value is inside a variable, just enter the variable name into the shell. Now here's something interesting. Because a variable is only a name for a value, we can write expressions with variables like this:
>>> spam = 15 >>> spam + spam 30 >>> spam - spam 0 >>>
When the variable spam has the integer value 15 stored in it, entering spam + spam is the same as entering 15 + 15, which evaluates to 30. And spam - spam is the same as 15 - 15, which evaluates to 0. The expressions above use the variable spam twice. You can use variables as many times as you want in expressions. Remember that Python will evaluate a variable name to the value that is stored inside that variable, each time the variable is used.
We can even use the value in the spam variable to assign spam a new value:
>>> spam = 15
>>> spam = spam + 5 20
>>>
The assignment statement spam = spam + 5 is like saying, “the new value of the spam variable will be the current value of spam plus five.” Remember that the variable on the left side of the = sign will be assigned the value that the expression on the right side evaluates to. We can also keep increasing the value in spam by 5 several times:
>>> spam = 15 >>> spam = spam + 5 >>> spam = spam + 5 >>> spam = spam + 5 >>> spam 30 >>>