Control Flow
Control flow decides which line of code runs and when. It helps your program:
- make decisions based on conditions
- repeat tasks efficiently
- stop or skip steps when needed
- handle different scenarios dynamically
if, elif, else Statements
Section titled “if, elif, else Statements”These statements are used to make decisions and control program flow.
if→ checks a condition (executes if True)elif→ checks another condition if the first is falseelse→ runs if all conditions are false
Order matters: elif and else blocks are optional, and multiple elif blocks can be chained.
x = 10
if x > 10: print("Greater than 10")elif x == 10: print("Equal to 10")else: print("Less than 10")Professional Pattern: Use elif for multiple conditions instead of nested if statements for better readability.
Flow of execution
Section titled “Flow of execution”Ternary Operator
Section titled “Ternary Operator”A concise way to write simple if-else statements in a single line. Also called conditional expression.
age = 18status = "Adult" if age >= 18 else "Minor"print(status) # Output: AdultSyntax: value_if_true if condition else value_if_false
Use Cases:
- Simple assignments based on conditions
- Default values
- Avoiding verbose if-else blocks
Gotcha: Avoid nesting multiple ternary operators as it reduces readability.
match-case Statement
Section titled “match-case Statement”Uses pattern matching to compare one value against multiple patterns. Available in Python 3.10+.
def check(value): match value: case 1: return "One" case 2: return "Two" case _: return "Other"
print(check(2)) # Output: TwoKey Features:
case pattern:→ matches a specific value_→ default case (matches anything)- More readable than many if-elif-else chains
- Better performance for many conditions
Advanced Patterns:
match point: case (0, 0): print("Origin") case (0, y): print(f"On Y axis: {y}") case (x, 0): print(f"On X axis: {x}") case _: print("General point")Flow idea
Section titled “Flow idea”for Loops
Section titled “for Loops”A for loop iterates through items in a sequence one by one. It’s the most common loop type in Python.
for i in range(3): print(i)Output:
012Iteration Over Collections:
names = ["A", "B", "C"]
for name in names: print(name)Professional Tips:
- Use
forloops instead ofwhilewhen iterating over sequences - Use
enumerate()when you need both index and value - Use
zip()to iterate over multiple sequences simultaneously - Prefer descriptive variable names:
for user in users:instead offor u in users:
How it works
Section titled “How it works”while Loops
Section titled “while Loops”A while loop runs repeatedly until the condition becomes false.
count = 0
while count < 3: print(count) count += 1Key Difference from for loops:
for→ iterate over known sequenceswhile→ loop until a condition is met- Risk of infinite loops if condition never becomes false
Common Patterns:
# Input validationwhile True: user_input = input("Enter a number: ") if user_input.isdigit(): break
# Event loop simulationrunning = Truewhile running: handle_events() update() render()break, continue, pass and else in Loops
Section titled “break, continue, pass and else in Loops”These control statements fine-tune loop behavior for complex scenarios.
Stops the loop immediately and exits completely.
for i in range(5): if i == 3: break # Exits loop when i equals 3 print(i)Output: 0 1 2
continue
Section titled “continue”Skips the current iteration and jumps to the next one.
for i in range(5): if i == 2: continue # Skips i=2 print(i)Output: 0 1 3 4
A null operation — does nothing when executed. Useful as a placeholder.
for i in range(5): if i == 2: pass # Placeholder, does nothing
if i == 3: pass # Also valid print(i)else in Loops
Section titled “else in Loops”Executes only if the loop completes normally without hitting break.
for i in range(3): print(i)else: print("Loop finished normally")
# Search examplefor user in users: if user.id == target_id: print("User found") breakelse: print("User not found")Flow with break
Section titled “Flow with break”range Function
Section titled “range Function”range() generates a sequence of numbers efficiently. It’s commonly used in loops.
for i in range(5): print(i) # 0, 1, 2, 3, 4Three Forms:
-
range(end)→ from 0 to end-1range(5) # 0, 1, 2, 3, 4 -
range(start, end)→ from start to end-1range(1, 6) # 1, 2, 3, 4, 5 -
range(start, end, step)→ from start to end-1, incrementing by steprange(0, 10, 2) # 0, 2, 4, 6, 8range(10, 0, -2) # 10, 8, 6, 4, 2 (reverse)
Professional Knowledge:
range()returns a range object (lazy evaluation, memory efficient)- Convert to list if needed:
list(range(5)) - Can be used with negative steps for iteration in reverse
enumerate Function
Section titled “enumerate Function”Adds an index (position) to each item when iterating. Essential for loops where you need both position and value.
items = ["a", "b", "c"]
for index, value in enumerate(items): print(index, value)Output:
0 a1 b2 cCustomizing Start Index:
for index, value in enumerate(items, start=1): print(index, value)Output:
1 a2 b3 cReal-World Example:
tasks = ["buy milk", "pay bills", "exercise"]
for num, task in enumerate(tasks, 1): print(f"{num}. {task}")# Output:# 1. buy milk# 2. pay bills# 3. exercisezip Function
Section titled “zip Function”Combines multiple sequences together element-by-element. Perfect for parallel iteration.
names = ["A", "B"]scores = [90, 80]
for name, score in zip(names, scores): print(name, score)Output:
A 90B 80Key Behaviors:
- Stops when shortest sequence ends
- Works with any iterable (lists, tuples, strings, etc.)
names = ["A", "B", "C"]scores = [90, 80] # Only 2 elements
for name, score in zip(names, scores): print(name, score)# Output:# A 90# B 80# C is skipped because no corresponding scorePractical Examples:
# Merge multiple listslist1 = [1, 2, 3]list2 = ['a', 'b', 'c']combined = list(zip(list1, list2))# Output: [(1, 'a'), (2, 'b'), (3, 'c')]
# Process related data in parallelstudents = ["Alice", "Bob", "Charlie"]gpa = [3.8, 3.5, 3.9]for student, grade in zip(students, gpa): print(f"{student}: {grade}")