Concepts / Variables and Assignment in Python

Variables and Assignment in Python

How It Works Here's how this program works. First, we assign the literal constant value 5 to the variable i using the assignment operator ( = ). This line is called a statement because it states that something should be done and in this case, we connect the variable name i to the value 5 . Next, we print the value of i using the print statement which, unsurprisingly, just prints the value of the variable to the screen.

  • 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.

binds toivariable name5value
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.

executesis used byprintsi = 5assignment statementi → 5name connected to valueprint(i)print statement5screen output
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.

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).

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

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

Display: 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.

python

Mistakes with Assignment

  • Treating i = 5 as if it prints a result.

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

    Fix: Use print(i) when the value associated with i should be printed to the screen.

  • Trying to update a variable before initializing it.

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

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

  • Ignoring statement order.

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

    Fix: Trace 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

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

Key Takeaways

  • 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.