Concepts / Variables and Assignment in Python

Variables and Assignment in Python

Assignment is also used to update a variable.

  • Programming

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.

ivariable name5valuebinds to
What is connected when Python executes i = 5?

Following the First Statement

What do you think happens?

After Python runs i = 5, what value will print(i) display?

  • i

  • 5

  • Nothing

Reveal answer
Answer:

5

The assignment connects the name i to the value 5. The print statement then prints the value associated with i.

i = 5assignment statementi → 5name connected to valueprint(i)print statement5screen outputexecutesis used byprints
What happens first when Python runs i = 5 followed by print(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.

Definition

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.

Worked example

Tracing i = 5 and print(i)

Determine what value is printed by the two statements i = 5 and print(i).

  1. 1
    Assign

    The assignment statement connects the variable name i to the literal value 5.

  2. 2
    Use

    The print statement refers to i and obtains the value connected to that name.

  3. 3
    Display

    The value obtained from i is printed to the screen.

Result

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.

python
Before
i = 5

i has its initial value

After
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 like
    i = 5
    Why it fails

    The assignment statement connects i to 5. The source explains that printing happens when the separate print statement is used.

    Do this instead

    Use 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 fails

    A variable must first be given an initial value before it can be updated.

    Do this instead

    Begin with a simple assignment that supplies the variable's initial value.

  • Common mistake

    Ignoring statement order.

    Why it fails

    The value used by print(i) comes from the earlier assignment i = 5.

    Do this instead

    Trace the statements in order: assign the value first, then print the variable.

Best practice

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

EASY

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

  1. 1
    The assignment operator connects a variable name to a value.
  2. 2
    In i = 5, the name i is connected to the value 5.
  3. 3
    Python executes the assignment before the later print statement uses the variable.
  4. 4
    print(i) prints the value associated with i.
  5. 5
    Initialization 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.

Related concepts

More concepts starting with V