Python Programming
Control Flow in Python
Control Flow in Python
Section titled “Control Flow in Python”Control flow statements allow you to control the execution order of your code.
Conditional Statements
Section titled “Conditional Statements”if, elif, else
Section titled “if, elif, else”age = 18
if age < 13: print("Child")elif age < 20: print("Teenager")else: print("Adult")Conditional Expressions (Ternary)
Section titled “Conditional Expressions (Ternary)”status = "adult" if age >= 18 else "minor"for Loop
Section titled “for Loop”# Iterating over a listfruits = ["apple", "banana", "cherry"]for fruit in fruits: print(fruit)
# Using rangefor i in range(5): print(i) # 0, 1, 2, 3, 4
# Enumerate for index and valuefor index, fruit in enumerate(fruits): print(f"{index}: {fruit}")while Loop
Section titled “while Loop”count = 0while count < 5: print(count) count += 1Loop Control
Section titled “Loop Control”# break - exit loopfor i in range(10): if i == 5: break print(i)
# continue - skip iterationfor i in range(5): if i == 2: continue print(i)
# else clause (runs if loop completes normally)for i in range(5): print(i)else: print("Loop completed!")