2.4. Variables¶
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.
Creating a variable consists of two steps, declaration and assignment.
Declaration statements act as notifications for the intent to use a variable and are used to help avoid using a variable incorrectly.
pi: float
n: float
A variable declaration consists of a name followed by : float
.
The above example declares two variables named n
and pi
. The
float part of the declaration refers to the type of information the
variable refers to, in this case numbers. In the future, we will see
several other kinds of variables.
Note
All variable declarations must come after any import statements and before any other lines of code in a program.
Assignment statements link a variable with a value.
pi: float
n: float
pi = 3.14159
n = 2.0 * 3.0
This example makes two assignments. The first assignment statement
links the name pi
with the value 3.14159. The second assignment
statement links the name n
with the value of the expression
2.0 * 3.0
, which is the value 6.0.
Note that the right side of the assignment statement can be any expression. But the variable will not be linked to the expression. The expression will first be evaluated, and then the variable will be linked to the resulting value of the expression.
The assignment operator, =
, should not be confused with
equality (later, we will see the Python equality operator, ==
).
The 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:
n: float
17.0 = n
Tip
When reading or writing code, say to yourself “n is assigned 17” or “n gets the value 17” or “n is a reference to the object 17” or “n refers to the object 17”. Don’t say “n equals 17” because this can be confused with the equality operator.
When a Python program evaluates a variable, it produces the value currently linked to the variable. In other words, evaluating a variable gives the value that is referred to by the variable.
Because variables evaluate to a value, they can be used in expressions. When a variable is used in an expression, the value the variable is linked to is used when computing the value of the expression. Variables can be, and often are, used in the expression of another variable’s value. The following example uses variables in arithmetic expressions:
In each case, the result is the value of the variable. To see this in even more detail, run the program using codelens by tapping the “Show CodeLens” button above. As you step through the statements, you can see the variables and the values they reference as those references are created.
Check your understanding
- Nothing is printed. An error occurs.
- The value is on the right side of the assignment operator so there is no error.
- float
- float is the type of the variable, which is used to prevent errors when the variable is used
- 32.5
- The variable length is assigned the value 32.5 before being printed.
- length
- length is the name of the variable. When a variable is used in an expression, the value of the variable is used.
data-4-1: What is printed when the following statements execute?
length: float
length = 32.5
print(length)
- Nothing is printed. An error occurs.
- The value must be on the right side of the assignment operator so.
- float
- float is the type of the variable, which is used to prevent errors when the variable is used
- 32.5
- The variable is never assigned the value 32.5 because the operator name must be on the left side of the assignment statement.
- length
- length is the name of the variable. When a variable is used in an expression, the value of the variable is used.
data-4-2: What is printed when the following statements execute?
length: float
32.5 = length
print(length)