Beginner
Control Flow
Learn how to make decisions with conditionals, repeat actions with loops, and write concise code with comprehensions.
if / elif / else Statements
Conditionals let your program make decisions based on conditions:
Python
age = 18 if age < 13: print("Child") elif age < 18: print("Teenager") elif age < 65: print("Adult") else: print("Senior") # Output: Adult
Indentation matters! Python uses indentation (4 spaces by convention) to define code blocks. Incorrect indentation causes
IndentationError.Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
< | Less than | 3 < 5 → True |
> | Greater than | 5 > 3 → True |
<= | Less than or equal | 5 <= 5 → True |
>= | Greater than or equal | 5 >= 3 → True |
Logical Operators
Python
x = 15 # and - both must be True if x > 10 and x < 20: print("Between 10 and 20") # or - at least one must be True if x < 5 or x > 10: print("Outside 5-10 range") # not - inverts the boolean is_raining = False if not is_raining: print("Go outside!") # Chained comparison (Pythonic) if 10 < x < 20: print("Between 10 and 20")
for Loops
Iterate over sequences (lists, strings, ranges, etc.):
Python
# Loop over a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Loop with index using enumerate for i, fruit in enumerate(fruits): print(f"{i}: {fruit}") # Loop over a string for char in "Python": print(char, end=" ") # P y t h o n # Loop over a dictionary scores = {"Alice": 95, "Bob": 87} for name, score in scores.items(): print(f"{name}: {score}")
range() Function
Python
# range(stop) for i in range(5): print(i, end=" ") # 0 1 2 3 4 # range(start, stop) for i in range(2, 6): print(i, end=" ") # 2 3 4 5 # range(start, stop, step) for i in range(0, 20, 5): print(i, end=" ") # 0 5 10 15 # Count backwards for i in range(5, 0, -1): print(i, end=" ") # 5 4 3 2 1
while Loops
Python
# Basic while loop count = 0 while count < 5: print(count, end=" ") count += 1 # 0 1 2 3 4 # User input loop while True: answer = input("Type 'quit' to exit: ") if answer == "quit": break
break, continue, pass
Python
# break - exit the loop entirely for i in range(10): if i == 5: break print(i, end=" ") # 0 1 2 3 4 # continue - skip to next iteration for i in range(10): if i % 2 == 0: continue print(i, end=" ") # 1 3 5 7 9 # pass - placeholder (do nothing) for i in range(5): pass # TODO: implement later
List Comprehensions
A concise, Pythonic way to create lists from loops:
Python
# Basic comprehension squares = [x**2 for x in range(10)] # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # With condition (filter) evens = [x for x in range(20) if x % 2 == 0] # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] # With transformation names = ["alice", "bob", "charlie"] capitalized = [name.capitalize() for name in names] # ['Alice', 'Bob', 'Charlie'] # Ternary in comprehension labels = ["even" if x % 2 == 0 else "odd" for x in range(5)] # ['even', 'odd', 'even', 'odd', 'even']
Nested Loops
Python
# Multiplication table for i in range(1, 4): for j in range(1, 4): print(f"{i} x {j} = {i * j}", end=" ") print() # New line # Nested list comprehension (flatten a matrix) matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flat = [num for row in matrix for num in row] # [1, 2, 3, 4, 5, 6, 7, 8, 9]
Pythonic tip: Prefer list comprehensions over manual for-loop appending when the logic is simple. But if a comprehension becomes hard to read, switch back to a regular loop.
Lilly Tech Systems