You're building a login system. If the password matches, show the dashboard. Otherwise, show "Invalid password". Programs need to take different paths based on conditions - that's what conditionals do.

Simple if

Execute code only when a condition is true.

x
simple.py
Replay: real traced execution (multi-file project)
x = 10

if x > 5:
    print("x > 5")

if x > 20:
    print("x > 20")
x = 3

if x > 5:
    print("x > 5")

if x > 20:
    print("x > 20")
x = 25

if x > 5:
    print("x > 5")

if x > 20:
    print("x > 20")
  1. x ← 10

    1x→ 10 = 10  #@x=3, 25
  2. if x > 5:

    3if x10 > 5:4    print("x > 5")
    outputx > 5
  1. x ← 3

    1x→ 3 = 3
  1. x ← 25

    1x→ 25 = 25
  2. if x > 5:

    3if x25 > 5:4    print("x > 5")
    outputx > 5
  3. if x > 20:

    6if x25 > 20:7    print("x > 20")
    outputx > 20

The code inside if only runs when the condition is true.

if `if condition:` - executes the block only when condition is true.
comparison `>` greater, `<` less, `>=` greater or equal, `<=` less or equal, `==` equal.

Find the maximum in a list

Scan through all elements, keep track of the largest one found.

max.py
Replay: real traced execution (multi-file project)
nums = [23, 45, 12, 67, 34, 89, 41]

print("nums=" + str(nums))
max = nums[0]

for i in range(1, len(nums)):

    if nums[i] > max:
        max = nums[i]

  1. nums ← [23, 45, 12, 67, 34, 89, 41], max ← 23

    1nums→ [23, 45, 12, 67, 34, 89, 41] = [23, 45, 12, 67, 34, 89, 41]23print("nums=" + str(nums[23, 45, 12, 67, 34, 89, 41]))4max→ 23 = nums[0]23
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for i in range(1, len(nums)): #?update_max

    pass 1 of 6
    6for i1 in range(1, len(nums[23, 45, 12, 67, 34, 89, 41])):7    #?update_max8    if nums[i] > max:9        max = nums[i]
    All 6 passes — pass 1 is the card above
    passi
    11
    22
    33
    44
    55
    66
  3. max ← 45

    pass 1 of 3
    7#?update_max8if nums[i]45 > max:9    max→ 45 = nums[i]45
    All 3 passes — pass 1 is the card above
    passnums[i]max
    14545
    26767
    38989

Compare each element to our current max. If bigger, update max. This is the find maximum pattern.

See the Branches

The max scan makes the branch visible: some values replace max, and others leave it alone. The diagrams pin the exact list from max.py.

The if branch decides update or keepThe if branch decides update or keepnums[i] > max?True: updateFalse: keepnext i
For each number, the condition `nums[i] > max` chooses one of two paths: update `max`, or keep the current value.
Max value after each comparisonMax value after each comparisoninums[i]branchmax afterstart23seed23145update45212keep45367update67434keep67589update89641keep89
Only 45, 67, and 89 are bigger than the best value so far. The final maximum is 89.

Find the minimum in a list

Same idea, but track the smallest.

min.py
Replay: real traced execution (multi-file project)
nums = [23, 45, 12, 67, 34, 89, 41]

print("nums=" + str(nums))
min = nums[0]

for i in range(1, len(nums)):
    if nums[i] < min:
        min = nums[i]
  1. nums ← [23, 45, 12, 67, 34, 89, 41], min ← 23

    1nums→ [23, 45, 12, 67, 34, 89, 41] = [23, 45, 12, 67, 34, 89, 41]23print("nums=" + str(nums[23, 45, 12, 67, 34, 89, 41]))4min→ 23 = nums[0]23
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for i in range(1, len(nums)):

    pass 1 of 6
    6for i1 in range(1, len(nums[23, 45, 12, 67, 34, 89, 41])):7    if nums[i] < min:8        min = nums[i]
    All 6 passes — pass 1 is the card above
    passinums[i]min
    11
    221212
    33
    44
    55
    66
  3. min ← 12

    6for i in range(1, len(nums)):7    if nums[i]12 < min:8        min→ 12 = nums[i]12

Same pattern, opposite comparison: update when you find something smaller.

else `else:` - executes when the if condition is false.

First N Fibonacci numbers

Generate a variable-length Fibonacci sequence.

n
fibonacci_n.py
Replay: real traced execution (multi-file project)
n = 12
fib = [0] * n

fib[0] = 0
fib[1] = 1


for i in range(2, n):
    fib[i] = fib[i - 1] + fib[i - 2]

print("fib=" + str(fib))

n = 5
fib = [0] * n

fib[0] = 0
fib[1] = 1


for i in range(2, n):
    fib[i] = fib[i - 1] + fib[i - 2]

print("fib=" + str(fib))

n = 20
fib = [0] * n

fib[0] = 0
fib[1] = 1


for i in range(2, n):
    fib[i] = fib[i - 1] + fib[i - 2]

print("fib=" + str(fib))

  1. n ← 12, fib ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], fib[0] ← 0

    1n→ 12 = 12  #@n=5, 202fib→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * n1234fib[0]→ 0 = 05fib[1]→ 1 = 1
  2. fib[i] ← 1

    pass 1 of 10
    7#?range_start8for i2 in range(2, n12):9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0
    All 10 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12101
    23112
    34213
    45325
    56538
    678513
    7813821
    89211334
    910342155
    1011553489
  3. print("fib=" + str(fib))

    11print("fib=" + str(fib[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]))
    outputfib=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  1. n ← 5, fib ← [0, 0, 0, 0, 0], fib[0] ← 0, fib[1] ← 1

    1n→ 5 = 52fib→ [0, 0, 0, 0, 0] = [0] * n534fib[0]→ 0 = 05fib[1]→ 1 = 1
  2. fib[i] ← 1

    pass 1 of 3
    8for i2 in range(2, n5):9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0
    All 3 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12101
    23112
    34213
  3. print("fib=" + str(fib))

    11print("fib=" + str(fib[0, 1, 1, 2, 3]))
    outputfib=[0, 1, 1, 2, 3]
  1. n ← 20, fib ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    1n→ 20 = 202fib→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * n2034fib[0]→ 0 = 05fib[1]→ 1 = 1
  2. fib[i] ← 1

    pass 1 of 18
    8for i2 in range(2, n20):9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0
    18 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12101
    23112
    34213
    45325
    56538
    678513
    7813821
    89211334
    910342155
    ⋯ 7 more passes ⋯
    171815979872584
    1819258415974181
  3. print("fib=" + str(fib))

    11print("fib=" + str(fib[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]))
    outputfib=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

The user specifies how many Fibonacci numbers they want. The condition i < n controls how many iterations run.

Search for a value in list

Check if a specific number exists.

target
search.py
Replay: real traced execution (multi-file project)
nums = [23, 45, 12, 67, 34, 89, 41]
target = 67

print("nums=" + str(nums))
found = 0
position = -1

for i in range(len(nums)):
    if nums[i] == target:
        found = 1
        position = i
nums = [23, 45, 12, 67, 34, 89, 41]
target = 12

print("nums=" + str(nums))
found = 0
position = -1

for i in range(len(nums)):
    if nums[i] == target:
        found = 1
        position = i
nums = [23, 45, 12, 67, 34, 89, 41]
target = 99

print("nums=" + str(nums))
found = 0
position = -1

for i in range(len(nums)):
    if nums[i] == target:
        found = 1
        position = i
  1. nums ← [23, 45, 12, 67, 34, 89, 41], target ← 67, found ← 0, position ← -1

    1nums→ [23, 45, 12, 67, 34, 89, 41] = [23, 45, 12, 67, 34, 89, 41]2target→ 67 = 67  #@target=12, 9934print("nums=" + str(nums[23, 45, 12, 67, 34, 89, 41]))5found→ 0 = 06position→ -1 = -1
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for i in range(len(nums)):

    pass 1 of 7
    8for i0 in range(len(nums[23, 45, 12, 67, 34, 89, 41])):9    if nums[i] == target:10        found = 1
    All 7 passes — pass 1 is the card above
    passinums[i]targetfoundposition
    10
    21
    32
    43676713
    54
    65
    76
  3. found ← 1, position ← 3

    8for i in range(len(nums)):9    if nums[i]67 == target67:10        found→ 1 = 111        position→ 3 = i3
  1. nums ← [23, 45, 12, 67, 34, 89, 41], target ← 12, found ← 0, position ← -1

    1nums→ [23, 45, 12, 67, 34, 89, 41] = [23, 45, 12, 67, 34, 89, 41]2target→ 12 = 1234print("nums=" + str(nums[23, 45, 12, 67, 34, 89, 41]))5found→ 0 = 06position→ -1 = -1
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for i in range(len(nums)):

    pass 1 of 7
    8for i0 in range(len(nums[23, 45, 12, 67, 34, 89, 41])):9    if nums[i] == target:10        found = 1
    All 7 passes — pass 1 is the card above
    passinums[i]targetfoundposition
    10
    21
    32121212
    43
    54
    65
    76
  3. found ← 1, position ← 2

    8for i in range(len(nums)):9    if nums[i]12 == target12:10        found→ 1 = 111        position→ 2 = i2
  1. nums ← [23, 45, 12, 67, 34, 89, 41], target ← 99, found ← 0, position ← -1

    1nums→ [23, 45, 12, 67, 34, 89, 41] = [23, 45, 12, 67, 34, 89, 41]2target→ 99 = 9934print("nums=" + str(nums[23, 45, 12, 67, 34, 89, 41]))5found→ 0 = 06position→ -1 = -1
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. for i in range(len(nums)):

    pass 1 of 7
    8for i0 in range(len(nums[23, 45, 12, 67, 34, 89, 41])):9    if nums[i] == target:10        found = 1
    All 7 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
    54
    65
    76

Loop through, check each element against the target. When found, we can stop early or record the position.