if, elif and else choose what runs; for with range() and while repeat it; break and continue change a loop; nested loops print patterns.
| range() | list(range(5)) = [0, 1, 2, 3, 4]; list(range(1, 10, 3)) = [1, 4, 7] |
| Adding 1 to n | sum of range(1, n + 1) = n(n + 1)/2: 1 to 10 gives 55 |
| Nested loops | range(3) inside range(4): the inner body runs 12 times |
| Special numbers | Armstrong: 153 = 13 + 53 + 33; perfect: 28 = 1 + 2 + 4 + 7 + 14 |
| break | for i in range(1, 30): if i * i > 50: break print(i) # 8 |
| continue | for i in range(1, 8): if i % 3 == 0: continue print(i, end=' ') # 1 2 4 5 7 |
What are a, b and c after these lines run?
a, b, c = 72, 52, 86
if a > b:
a, b = b, a
if b > c:
b, c = c, b
if a > b:
a, b = b, a
Working: each swap puts the smaller value first: the three numbers end up sorted
Answer: a = 52, b = 72, c = 86