Skip to course content
Free course

Python Foundations / Module 2 / Variables and Assignment

Module 2 lesson

Variables and Assignment

Unit ID: M02-U01 Estimated active time: 18-25 minutes

A name refers to a value

Run:

course_hours = 32

The assignment makes the name course_hours refer to the integer value 32.

The equals sign is the assignment operator. Read the line as:

Make course_hours refer to 32.

Do not read it as a permanent mathematical equality.

Reassignment changes the current reference

course_hours = 32
course_hours = 30
print(course_hours)

The output is 30. The second assignment changes what the name currently refers to.

This is why execution order matters. If you run the first line again after the second, the current value becomes 32.

Use the old value to create a new one

completed_hours = 6
completed_hours = completed_hours + 2
print(completed_hours)

Python reads the current value 6, adds 2, then assigns the result 8 back to the name.

One value can have more than one name

planned_hours = 32
display_hours = planned_hours

Both names currently refer to the integer 32. Reassigning one name does not automatically reassign the other:

planned_hours = 30
print(planned_hours)
print(display_hours)

Output:

30
32

This example uses immutable number values. Shared mutable collections require more care and will appear in Module 3.

Assignment does not display automatically

course_name = "Python Foundations for AI"

The assignment usually has no visible output. Inspect the value by writing the name as the final notebook expression or by using print(course_name).

Trace the state

Predict the final value:

lesson_count = 5
lesson_count = lesson_count + 1
review_count = lesson_count
lesson_count = 8
print(lesson_count)
print(review_count)

The outputs are 8 and 6.

Write a tracing table with one row per assignment. This makes the changing state visible.

Naming reminder

Use lowercase snake_case names that describe the role of the value. Avoid names that hide built-ins, such as str, int, type, or print. A name like course_type is clear; naming a variable type would make later code confusing.

Takeaway

A variable is a name that currently refers to a value. Assignment and reassignment happen in execution order. Trace the current value rather than treating a name as a permanent box. Next, we will use numeric types and operators deliberately.