Variables and Assignment in Python
Assignment is also used to update a variable.
A Name Connected to a Value
A Python variable gives a name to a value that a program can use. In the statement i = 5, the name i is connected to the value 5. The equals sign is the assignment operator: it tells Python to make that connection.
Following the First Statement
What do you think happens?
After Python runs i = 5, what value will print(i) display?
Reveal answer
5
The assignment connects the name i to the value 5. The print statement then prints the value associated with i.
Python follows the statements in order. First, i = 5 is executed, so the name i becomes connected to 5. Next, print(i) uses that connection and prints 5 to the screen. The print statement does not create the connection; the assignment statement creates it, and print uses it.
Assignment Statements
An assignment statement states that something should be done. In the example i = 5, Python assigns the literal constant value 5 to the variable i. The central action is connecting the variable name i to that value.
Initialization is an assignment that gives an initial value to a variable that will later be updated.
Initialization matters when a variable will change during a program. Before a variable can be updated, it must first receive an initial value, usually through a simple assignment.
Tracing i = 5 and print(i)
Determine what value is printed by the two statements i = 5 and print(i).
- 1Assign
The assignment statement connects the variable name i to the literal value 5.
- 2Use
The print statement refers to i and obtains the value connected to that name.
- 3Display
The value obtained from i is printed to the screen.
5 is printed.
Updating an Existing Variable
Assignment is also used to update a variable. In an update, the new value depends on the old value. This is different from the first assignment, which initializes the variable by giving it its initial value.
i = 5i has its initial value
i = 5
# Later, an assignment can update i using its old value.i is ready to be updated
Mistakes with Assignment
Common mistake Treating i = 5 as if it prints a result.
What it looks likei = 5Why it failsThe assignment statement connects i to 5. The source explains that printing happens when the separate print statement is used.
Do this insteadUse print(i) when the value associated with i should be printed to the screen.
Common mistake Trying to update a variable before initializing it.
Why it failsA variable must first be given an initial value before it can be updated.
Do this insteadBegin with a simple assignment that supplies the variable's initial value.
Common mistake Ignoring statement order.
Why it failsThe value used by
print(i)comes from the earlier assignment i = 5.Do this insteadTrace the statements in order: assign the value first, then print the variable.
When reading a short Python program, identify each assignment first. Record which value is connected to each variable name, then follow later statements that use or update those variables.
Check Your Understanding
Trace these statements in order: i = 5, followed by print(i). State what connection is created by the first statement and what appears on the screen after the second statement runs.
Hints
- Identify the value assigned to i.
- Then determine which value
print(i)uses.
A complete answer should say that i is connected to 5 by the assignment and that print(i) displays 5. For a variable intended to change later, also identify the first assignment as its initialization.
Key Takeaways
- 1The assignment operator connects a variable name to a value.
- 2In i = 5, the name i is connected to the value 5.
- 3Python executes the assignment before the later print statement uses the variable.
- 4
print(i)prints the value associated with i. - 5Initialization gives a variable its first value; later assignment can update it using its old value.
At a glance
- A variable name can be connected to a value through assignment.
- The statement i = 5 assigns the value 5 to i.
- Python then uses that connection when
print(i)runs. - A variable that will be updated must first be initialized.