Skip to course content
Free course

Python Foundations / Module 1 / Read Code Before Running It

Module 1 lesson

Read Code Before Running It

Unit ID: M01-U04 Estimated active time: 18-25 minutes

Running code is not the same as understanding it

A code cell can produce the expected output even when the learner cannot explain why. This becomes dangerous when the code is changed, copied from another source, or generated by an AI tool.

Before you run a short example, trace it line by line.

ticket_price = 250
ticket_count = 3
total = ticket_price * ticket_count
print(total)

Read it aloud in plain language:

  1. ticket_price refers to 250.
  2. ticket_count refers to 3.
  3. total becomes 250 multiplied by 3.
  4. Python prints 750.

This plain-language trace is a small algorithm: an ordered explanation of the work.

Prediction 1

What will this code display?

base = 20
increase = 5
base = base + increase
print(base)

The answer is 25. The third line uses the current value of base, adds increase, and then makes base refer to the new result.

The equals sign in Python assignment does not mean "is permanently equal to". It tells Python to make a name refer to a value.

Prediction 2

minutes = 45
hours = minutes / 60
print(hours)

The result is 0.75.

Do not stop at the number. State what it means: 45 minutes is 0.75 hours.

Prediction 3: look for the first failure

price = 100
total = price + delivery
delivery = 20
print(total)

The code fails on the second line because delivery has not been created yet. Python does not skip ahead to search for a later definition.

A simple tracing table

For short programs, make a table before running:

LineName changedNew valueExpected output
base = 20base20None
increase = 5increase5None
base = base + increasebase25None
print(base)NoneNone25

You will not need a table forever. It is a temporary support for making invisible state visible.

Independent practice

Predict the output of each program before running it.

items = 4
price_each = 75
amount = items * price_each
print(amount)
score = 10
score = score + 2
score = score * 3
print(score)
first = "Python"
second = "practice"
print(first + " " + second)

Expected outputs are 300, 36, and Python practice.

If your prediction differed, identify the exact line where your mental result changed from Python's result.

When code comes from an AI tool

Treat generated code as a suggestion, not as trusted instructions. Read each line, identify every input, and predict important outputs before running it. Never run generated code that accesses files, accounts, networks, or system settings unless you understand and authorise those actions.

Takeaway

Prediction turns execution into evidence. Trace names and values in order, identify the first possible failure, and explain the result in the task's real meaning. Next, we will write a small readable program of our own.