FreeCampus Python

Values, Names, and Assignment

Build a reliable model of values, names, assignment, reassignment, and program state by tracing practical calculations line by line.
python-foundations python-syntax values assignment
Open in Colab
  • Level: Beginner
  • Estimated time: 2.5–3.5 hours
  • You will learn: Explain how Python creates values, binds names, looks up names, and updates program state through assignment.
  • Practice in: Google Colab, JupyterLab, or a local editor

A useful program needs to remember facts. A study planner remembers a course name and a session length. A shop program remembers prices and quantities. A game remembers a score. Python lets us attach readable names to those values, then use the names in later lines.

NoteQuestions you will answer

By the end of the lesson, you should be able to explain:

  1. What is the difference between a value and a name?
  2. What does Python do with the right side of an assignment?
  3. Why can the same name produce different values at different times?
  4. How can a trace table reveal a mistake before the final output?

1. Begin with values Python can use

A value is a piece of information Python can work with. Enter each example in a separate cell:

45
"Python Foundations"
3.5
True

A value written directly in source code is called a literal. The characters 45 are an integer literal. The quotation marks in "Python Foundations" tell Python to create a text value. True is Python’s spelling for one of its two boolean values.

For now, concentrate on reading the values rather than memorizing every type. Unit 2 studies numbers, text, booleans, conversion, and input in depth.

Values are not automatically displayed in a program

A notebook often displays the value of the last expression in a cell:

45

A normal Python file does not display every value it creates. Use print() when displaying a value is part of the program:

print(45)
print("Python Foundations")

Line by line:

  1. Python sees the name print.
  2. The parentheses contain the value to give to print.
  3. The first call displays 45.
  4. The second call displays Python Foundations without the quotation marks. The marks describe the text in source code; they are not part of the text.
WarningNotebook display is not the same as print

A notebook may show the last value in a cell as a convenience. If the output is important to your program, call print() explicitly. Later you will learn that a value can be calculated, stored, returned, or displayed—these are different actions.

Try three different uses

Run this cell:

print("Session minutes:")
print(45)
print(30 + 15)

All three lines display something, but the values reach print() differently:

  • the first is a text literal;
  • the second is an integer literal;
  • the third is calculated from an expression.

Change only 15 to 20. Predict the final line before running it.

2. Give important values readable names

A literal is useful once. A name makes the value easy to reuse and change.

course = "Python Foundations"
session_minutes = 45

print(course)
print(session_minutes)

The first line is an assignment statement. Read it as:

Bind the name course to the value "Python Foundations".

Do not read = as “is already equal to.” In Python, one equals sign performs assignment.

The second assignment binds session_minutes to 45. The calls to print() then ask Python to look up the value currently bound to each name.

What Python does on each line

Consider a small shop calculation:

notebook_price = 4
notebook_count = 3
subtotal = notebook_price * notebook_count
print(subtotal)

Python handles it in this order:

  1. Create the value 4, then bind notebook_price to it.
  2. Create the value 3, then bind notebook_count to it.
  3. Look up notebook_price and obtain 4.
  4. Look up notebook_count and obtain 3.
  5. Multiply the two values and produce 12.
  6. Bind subtotal to 12.
  7. Look up subtotal and display 12.

The names make the calculation explain itself. Compare it with:

print(4 * 3)

Both versions display 12. Only the first version preserves what 4, 3, and 12 mean.

3. Assignment evaluates the right side first

The assignment pattern is:

name = expression

Python evaluates the expression on the right before it changes the name on the left.

minutes = 20 + 25
print(minutes)

Python adds 20 + 25, obtains 45, and only then binds minutes to 45.

The right side can use names that already exist:

session_minutes = 45
break_minutes = 10
focused_minutes = session_minutes - break_minutes

print(focused_minutes)

Before running, predict the value of focused_minutes.

Line by line:

  1. session_minutes becomes bound to 45.
  2. break_minutes becomes bound to 10.
  3. Python looks up both names and calculates 45 - 10.
  4. focused_minutes becomes bound to the result, 35.
  5. The final line displays 35.

The diagram follows assignment from the written line to the resulting binding.

flowchart LR
  A["focused_minutes = session_minutes - break_minutes"] --> B["Look up session_minutes → 45"]
  B --> C["Look up break_minutes → 10"]
  C --> D["Evaluate 45 - 10 → 35"]
  D --> E["Bind focused_minutes → 35"]

The left side is not read like the right side

This line is valid:

score = 8

This reversed version is not assignment:

8 = score

The left side must identify a place Python can bind the result. A number literal cannot become a name.

Check your understanding

4. Trace state instead of guessing

A program’s state is the collection of relevant name-to-value bindings at a particular moment. A trace table records that state after each line.

pages_read = 12
pages_read = pages_read + 5
pages_remaining = 30 - pages_read
print(pages_remaining)

Before running, complete the table:

After line pages_read pages_remaining
1 12 not assigned
2 ? not assigned
3 ? ?
4 ? ?

The completed trace is:

After line pages_read pages_remaining
1 12 not assigned
2 17 not assigned
3 17 13
4 17 13

The fourth line displays a value but does not change either binding.

Reassignment changes what a name finds next

This line often surprises people seeing programming for the first time:

pages_read = pages_read + 5

It is not an equation claiming that a number equals itself plus five. It is a sequence of actions:

  1. Look up the current value of pages_read: 12.
  2. Add 5: the result is 17.
  3. Bind pages_read to 17.
  4. Future lookups of pages_read find 17.

Reassignment uses the old binding to calculate a new one.

flowchart LR
  A["pages_read → 12"] --> B["Evaluate pages_read + 5"]
  B --> C["12 + 5 → 17"]
  C --> D["pages_read → 17"]

The earlier line of source code has not changed. The program has moved to a new state.

A calculated value does not update by itself

Predict the output:

price = 5
quantity = 2
total = price * quantity

quantity = 4
print(total)

The output is 10, not 20. total = price * quantity ran only once. Changing quantity later does not make Python revisit earlier assignments.

To refresh the total, calculate it again:

price = 5
quantity = 2
total = price * quantity

quantity = 4
total = price * quantity
print(total)

Now the output is 20.

This behavior matters in notebooks. If you run cells out of order, a result may have been calculated from older values. Restarting and running from the top reveals the true sequence.

5. Names must exist before Python can read them

Python can only look up a name after an executed assignment has created its binding.

course = "Python Foundations"
print(course)

Reverse the order:

print(course_name)
course_name = "Python Foundations"

If no earlier cell created course_name, Python raises:

NameError: name 'course_name' is not defined

The source is grammatically valid, so this is not a SyntaxError. Python understood the instruction but could not find the requested name while running it.

Similar-looking names are still different

session_minutes = 45
print(session_minute)

The assignment uses plural minutes; the lookup uses singular minute. Python does not guess that they were intended to match.

Case also matters:

course = "Python Foundations"
print(Course)

course and Course are two different names.

Find the first inconsistent name

Read this without running:

ticket_price = 12
ticket_count = 3
ticket_total = ticket_price * tickets_count
print(ticket_total)

Which name is inconsistent? The third line asks for tickets_count, but the second line created ticket_count.

Make the smallest repair. Do not rename every variable; change the one use that does not match the established name.

TipRead NameError from the bottom up

Start with the final error line to learn the error type and missing name. Then inspect the named source line and the assignments above it. Look for spelling, pluralization, capitalization, and execution-order differences.

6. Two names can refer to equal values

Run:

morning_minutes = 30
evening_minutes = 30

print(morning_minutes)
print(evening_minutes)

The two names have the same numeric value, but they represent different facts. Changing one binding does not change the other:

morning_minutes = 30
evening_minutes = 30

morning_minutes = 45

print(morning_minutes)
print(evening_minutes)

The output is:

45
30

This is why names describe roles, not only values. A program may need separate names even when their current values happen to match.

Copying a value into another name

planned_minutes = 45
actual_minutes = planned_minutes

planned_minutes = 60

print(planned_minutes)
print(actual_minutes)

When actual_minutes = planned_minutes runs, Python looks up planned_minutes and binds actual_minutes to the resulting value, 45. Reassigning planned_minutes later does not automatically rerun the copy.

For simple number and text examples, think of the trace as:

Line planned_minutes actual_minutes
1 45 not assigned
2 45 45
3 60 45

Collections and mutable objects add an important sharing question. Unit 6 treats that topic carefully; do not generalize this simple trace to every Python object yet.

Check your understanding

7. Choose names that make change safer

These names are legal but unhelpful:

x = 45
y = 10
z = x - y
print(z)

This version exposes the program’s meaning:

session_minutes = 45
break_minutes = 10
focused_minutes = session_minutes - break_minutes
print(focused_minutes)

A useful beginner naming checklist:

  • use lowercase words separated by underscores: focused_minutes;
  • describe the value’s role, not only its type: course_name, not text;
  • include units when confusion is possible: duration_seconds;
  • use the same spelling everywhere;
  • avoid one-letter names except in a tiny mathematical example;
  • avoid names that Python already uses for common tools, such as print.

Lesson 3 gives the complete rules for valid identifiers and reserved keywords.

Rename without changing behavior

Improve this program:

a = 7
b = 6
c = a * b
print(c)

Choose one interpretation, such as boxes and items per box. Rename all four uses consistently. The output should remain 42.

Show one possible revision
box_count = 7
items_per_box = 6
item_total = box_count * items_per_box
print(item_total)

The new names expose the meaning of the multiplication. Different names are fine if they tell a coherent story and every use is updated.

8. Build a study-session summary

Create a new cell with this starter code:

course_name = "Python Foundations"
planned_minutes = 50
break_minutes = 10

focused_minutes = planned_minutes - break_minutes

print("Course:")
print(course_name)
print("Focused minutes:")
print(focused_minutes)

Before running, write the four displayed lines in order.

Then complete these changes one at a time:

  1. Change planned_minutes to 65; predict the new focused time.
  2. Add review_minutes = 15.
  3. Add total_learning_minutes = focused_minutes + review_minutes.
  4. Display a label and the new total.
  5. Change break_minutes after the totals are calculated. Observe which stored values become stale.
  6. Move the calculations to the correct place so every result reflects the latest inputs.
  7. Rename one value consistently without changing the output.

Use a trace table to verify the final version

Your final table should have one row after each assignment:

Line planned_minutes break_minutes focused_minutes review_minutes total_learning_minutes
1 value
2 value value

Do not fill a cell from memory. Read the program in execution order and update only the binding changed by that line.

Hint: a reliable calculation order

Assign the input facts first. Calculate focused_minutes only after the final values of planned_minutes and break_minutes are available. Calculate total_learning_minutes only after both of its inputs are available. Display the results last.

Show one complete version
course_name = "Python Foundations"
planned_minutes = 65
break_minutes = 10
review_minutes = 15

focused_minutes = planned_minutes - break_minutes
total_learning_minutes = focused_minutes + review_minutes

print("Course:")
print(course_name)
print("Focused minutes:")
print(focused_minutes)
print("Total learning minutes:")
print(total_learning_minutes)

The output is:

Course:
Python Foundations
Focused minutes:
55
Total learning minutes:
70

9. Diagnose three state mistakes

For each example:

  1. predict the output or error;
  2. identify the first line that does not match the intended story;
  3. make one repair;
  4. rerun from the top.

The calculation happens too early

adult_tickets = 2
ticket_total = adult_tickets * 12
adult_tickets = 3

print(ticket_total)

If the intended total is for three tickets, move or repeat the calculation after the final assignment to adult_tickets.

The same fact has two spellings

workshop_minutes = 90
break_minute = 15
active_minutes = workshop_minutes - break_minutes

print(active_minutes)

Choose either singular or plural and use it consistently.

A result overwrites an input

distance_km = 12
distance_km = distance_km * 2
print(distance_km)

The code is valid, and doubling may even be intended. But if the program needs both the one-way distance and round-trip distance, preserve both meanings:

one_way_km = 12
round_trip_km = one_way_km * 2
print(round_trip_km)

The best repair depends on the requirement, not only on what Python accepts.

10. Check your model of assignment

Key points

TipKey points
  • A literal writes a value directly in source code.
  • Assignment evaluates the right side, then binds its result to the name on the left.
  • Looking up a name returns the value currently bound to it.
  • Reassignment changes a binding for future lookups; it does not rewrite earlier source code.
  • Calculated results do not refresh automatically when an input changes.
  • Trace tables make state and execution order visible.
  • Good names preserve meaning and make later changes safer.
  • Detailed types, conversion, and user input belong to Unit 2.

References

Back to top