Conditions, loops and functions cheat sheet
Making decisions and repeating code. Each example shows the code and exactly what it prints.
x = 15 if x > 10: print("big") elif x > 5: print("medium") else: print("small") | big | if / elif / else |
for i in range(3): print(i) | 0 1 2 | Count 0, 1, 2 |
print(list(range(2, 11, 4))) | [2, 6, 10] | range(start, stop, step) |
for i, fruit in enumerate(["mango", "guava"]): print(i, fruit) | 0 mango 1 guava | Position and item |
n = 1 while n < 50: n *= 3 print(n) | 81 | while loop |
for i in range(10): if i == 3: break print(i) | 0 1 2 | break leaves the loop |
def area(w, h=1): return w * h print(area(3, 4), area(5)) | 12 5 | Function with a default value |
try: int("x") except ValueError: print("not a number") | not a number | Handling errors |
Py-CRAFT cheat sheet. © 2026 Dr. Saptarshi Das (PhD, IIT Kharagpur | ME, Jadavpur University). All rights reserved.