Section 2.3 Variables
Up to this point, our programs have been manipulating numbers directly; for example, the program to determine someone’s approximate age in days:
print("My age in days is about")
print(27 * 365)
One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value. Take a look at this calculation from another program:
print("The dew point is ")
print(19 - (100 - 47.5) / 5)
Without names attached to these values, it’s probably very difficut for you to figure out what the numbers in this formula represent. If, however, we attach names to these values, the role of the numbers becomes much clearer:
temperature_celsius = 19
relative_humidity = 47.5
dew_point = temperature_celsius - (100 - relative_humidity) / 5
print("The dew point is", dew_point)
Even in the “age in days” program, adding variables makes our intent somewhat clearer:
The first two lines of the preceding program are assignment statements. Assignment statements create new variables and also give them values to refer to.
Line one assigns the value 27 to a new variable named years
. Line two assigns the value of the calculation to a new variable named days
. From this, we see that an assignment can use previously-established variables to calculate the values for new variables.
The =
in each of the assignments is called the assignment operator.
An assignment statement links a name, on the left hand side of the operator, with a value, on the right hand side. This is why you will get an error if you enter:
You can assign string values to variables as well as numbers:
message = "What's up, Doc?"
years = 27
price = 11.95
A common way to represent variables on paper is to write the name with an arrow pointing to the variable’s value. This kind of figure, known as a reference diagram, is often called a state snapshot because it shows what state each of the variables is in at a particular instant in time. (Think of it as the variable’s state of mind). This diagram shows the result of executing the assignment statements shown above.
If you ask Python to evaluate a variable, it will produce the value that is currently linked to the variable. In other words, evaluating a variable will give you the value that is referred to by the variable.
In each case the result is the value of the variable. To see this in even more detail, we can run the program using codelens.
Now, as you step through the statements, you can see the variables and the values they reference as those references are created.
We can also use variables in a program to “remember” things, like the current score at the football game. But variables are variable. This means they can change over time, just like the scoreboard at a football game. You can assign a value to a variable, and later assign a different value to the same variable.
To see this, read and then run the following program. You’ll notice we change the value of day
three times, and on the third assignment we even give it a value that is of a different type.
A great deal of programming is about having the computer remember things. For example, we might want to keep track of the number of missed calls on your phone. Each time another call is missed, we will arrange to update or change the variable so that it will always reflect the correct value.
Checkpoint 2.3.3.
What is printed when the following statements execute?
weight = "heavy"
weight = 32.5
weight = 19
print(weight)
Nothing is printed. A runtime error occurs.
It is legal to change the type of data that a variable holds in Python.
Thursday
This is the first value assigned to the variable weight
, but the next statements reassign that variable to new values.
32.5
This is the second value assigned to the variable weight
, but the next statement reassigns that variable to a new value.
19
The variable weight
will contain the last value assigned to it when it is printed.