Execution Flow

Concepts covered: Conditionals

Python runs a program one line at a time from the top, and an if statement is the first place the path forks. Reaching it, Python evaluates the condition once, using whatever each variable holds at that moment. It runs the block that result selects and then continues below. It never goes back to re-check a condition it has passed, even when a later line changes the variable.

A full trace

The program below buys an item, tries to buy a second, and then resets the balance. Before you play it, write down what you expect each print to show. The second if reads balance after the first block changed it, and the final assignment to 200 comes too late to affect either decision.

balance = 80
price = 50

if balance >= price:
    balance = balance - price
    print("Purchased, balance:", balance)

if balance >= price:
    print("Second purchase ok")
else:
    print("Not enough for a second")

balance = 200
print("Final balance:", balance)
Purchased, balance: 30
Not enough for a second
Final balance: 200
LineWhat Python doesbalance after
`balance = 80`stores 8080
`price = 50`stores 5080
`if balance >= price:`80 >= 50 is True, enters the block80
`balance = balance - price`computes 80 - 5030
`print(...)`prints Purchased, balance: 3030
`if balance >= price:`30 >= 50 is False, jumps to else30
`print(...)`prints Not enough for a second30
`balance = 200`stores 200, no condition is re-run200
`print(...)`prints Final balance: 200200

The trace table is the tool to reach for whenever a program surprises you. One row per executed line, with the variable values after it, turns a guess into a record you can check against the real output. Lines inside a skipped block get no row at all, because Python never executed them.

Indentation decides what an if owns

Python has no other way to know where a block ends. Each indented line after the header belongs to the if, and the first line back at the margin belongs to the program. Moving a single line left or right by 4 spaces changes which lines the condition controls, as the next 2 programs show. They differ only in the indent of the third print.

credits = 2

if credits >= 5:
    print("Premium tier")
    print("Unlocked all features")
print("Credits:", credits)

Output:

Credits: 2

With 2 credits, 2 >= 5 is False, so both messages inside the block are skipped and only the credits line prints. The second program is identical except that the unlock message has moved 4 spaces to the left.

credits = 2

if credits >= 5:
    print("Premium tier")
print("Unlocked all features")
print("Credits:", credits)

Output:

Unlocked all features
Credits: 2

That single move took the unlock message out of the block. It now sits at the margin and prints for every user, including one with 2 credits. The condition is the same in both programs, but it now controls 1 fewer line.

Blocks inside blocks

An if block can hold another if. The inner if sits at 4 spaces, inside the outer block, and its own block is indented 8. Each level of nesting adds 4 more spaces, so the depth of the indent tells you how many conditions a line sits under. The inner condition is evaluated only when the outer one was True.

logged_in = True
is_admin = False

if logged_in:
    print("Dashboard loaded")
    if is_admin:
        print("Admin panel shown")
    print("Profile shown")
print("Page rendered")

Output:

Dashboard loaded
Profile shown
Page rendered

logged_in is True, so Python enters the outer block and prints the dashboard line. It then reaches the inner if, finds is_admin to be False, and skips the admin line. The profile line is indented 4 spaces, so it is back in the outer block and prints. The last line sits at the margin and prints for everyone. Had logged_in been False, Python would have jumped from the outer header straight to that last line, and the inner condition would never have been evaluated.

What does this program print?

count = 3

if count > 2:
    count = count + 10
if count > 5:
    print("Big")
else:
    print("Small")
print(count)

When a line that should always run prints only some of the time, look at its indent before its logic. A line indented under an if runs only when the condition is True, so a summary meant for every customer goes at the left margin, after the block ends.

About this lesson

This section is part of the Control Flow: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.