Foundations
Conditionals
Make Decisions
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 = 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")
x ← 10
1x→ 10 = 10 #@x=3, 25if x > 5:
3if x10 > 5:4 print("x > 5")outputx > 5
x ← 3
1x→ 3 = 3
x ← 25
1x→ 25 = 25if x > 5:
3if x25 > 5:4 print("x > 5")outputx > 5if x > 20:
6if x25 > 20:7 print("x > 20")outputx > 20
The code inside if only runs when the condition is true.
Find the maximum in a list
Scan through all elements, keep track of the largest one found.
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]
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]23outputnums=[23, 45, 12, 67, 34, 89, 41]for i in range(1, len(nums)): #?update_max
pass 1 of 66for 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 pass i1 1 2 2 3 3 4 4 5 5 6 6 max ← 45
pass 1 of 37#?update_max8if nums[i]45 > max:9 max→ 45 = nums[i]45All 3 passes — pass 1 is the card above pass nums[i]max1 45 45 2 67 67 3 89 89
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.
Find the minimum in a list
Same idea, but track the smallest.
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]
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]23outputnums=[23, 45, 12, 67, 34, 89, 41]for i in range(1, len(nums)):
pass 1 of 66for 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 pass inums[i]min1 1 — — 2 2 12 12 3 3 — — 4 4 — — 5 5 — — 6 6 — — 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.
First N Fibonacci numbers
Generate a variable-length Fibonacci sequence.
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))
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 = 1fib[i] ← 1
pass 1 of 107#?range_start8for i2 in range(2, n12):9 fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0All 10 passes — pass 1 is the card above pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 1 2 3 1 1 2 3 4 2 1 3 4 5 3 2 5 5 6 5 3 8 6 7 8 5 13 7 8 13 8 21 8 9 21 13 34 9 10 34 21 55 10 11 55 34 89 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]
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 = 1fib[i] ← 1
pass 1 of 38for i2 in range(2, n5):9 fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0All 3 passes — pass 1 is the card above pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 1 2 3 1 1 2 3 4 2 1 3 print("fib=" + str(fib))
11print("fib=" + str(fib[0, 1, 1, 2, 3]))outputfib=[0, 1, 1, 2, 3]
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 = 1fib[i] ← 1
pass 1 of 188for i2 in range(2, n20):9 fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]018 passes — pass 1 is the card above pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 1 2 3 1 1 2 3 4 2 1 3 4 5 3 2 5 5 6 5 3 8 6 7 8 5 13 7 8 13 8 21 8 9 21 13 34 9 10 34 21 55 ⋯ 7 more passes ⋯ 17 18 1597 987 2584 18 19 2584 1597 4181 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.
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
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 = -1outputnums=[23, 45, 12, 67, 34, 89, 41]for i in range(len(nums)):
pass 1 of 78for i0 in range(len(nums[23, 45, 12, 67, 34, 89, 41])):9 if nums[i] == target:10 found = 1All 7 passes — pass 1 is the card above pass inums[i]targetfoundposition1 0 — — — — 2 1 — — — — 3 2 — — — — 4 3 67 67 1 3 5 4 — — — — 6 5 — — — — 7 6 — — — — found ← 1, position ← 3
8for i in range(len(nums)):9 if nums[i]67 == target67:10 found→ 1 = 111 position→ 3 = i3
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 = -1outputnums=[23, 45, 12, 67, 34, 89, 41]for i in range(len(nums)):
pass 1 of 78for i0 in range(len(nums[23, 45, 12, 67, 34, 89, 41])):9 if nums[i] == target:10 found = 1All 7 passes — pass 1 is the card above pass inums[i]targetfoundposition1 0 — — — — 2 1 — — — — 3 2 12 12 1 2 4 3 — — — — 5 4 — — — — 6 5 — — — — 7 6 — — — — found ← 1, position ← 2
8for i in range(len(nums)):9 if nums[i]12 == target12:10 found→ 1 = 111 position→ 2 = i2
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 = -1outputnums=[23, 45, 12, 67, 34, 89, 41]for i in range(len(nums)):
pass 1 of 78for i0 in range(len(nums[23, 45, 12, 67, 34, 89, 41])):9 if nums[i] == target:10 found = 1All 7 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 6 5 7 6
Loop through, check each element against the target. When found, we can stop early or record the position.