4.1 if, elif, and else
Python makes decisions with if, optional elif (short for "else if," used for additional conditions), and an optional final else that catches everything not covered above it.
As with everything else in Python, there are no curly braces marking each branch — indentation alone tells Python which lines belong to which condition.
Grading Example from the Guide
# GRADING EXAMPLE
marks = 82
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
else:
grade = "C"
print(grade) # "B"
Does Python Have a Switch/Case Statement?
Python also has no switch/case statement in the classic sense; long chains of elif, a dictionary that maps values to functions, or (from Python 3.10 onward) the newer match/case statement all fill that role instead.
# Python 3.10+ Structural Pattern Matching
http_code = 404
match http_code:
case 200:
print("OK")
case 400 | 404:
print("Client Error")
case 500:
print("Server Error")
case _:
print("Other Code") # Wildcard fallback
Truthy and Falsy Values in Python
In Python, every object has an inherent boolean truth value. The following are considered Falsy in conditional tests:
- Constants:
NoneandFalse - Zero of any numeric type:
0,0.0,0j - Empty sequences and collections:
"",(),[],{},set()
Everything else is considered Truthy!
4.2 Loops: for and while
Python's for loop works differently from the counting loops in C or Java. Rather than manually incrementing an index variable, a Python for loop iterates directly over the elements of anything "iterable" — a list, a string, a dictionary, an open file, or a range of numbers.
When you specifically need a counter, range(5) generates the numbers 0 through 4 for you to loop over.
The while loop, in contrast, behaves exactly as it does in C: it keeps repeating for as long as its condition remains True. It's the right tool whenever you don't know in advance how many times you'll need to repeat something.
Code Example from the Guide
# FOR VS. WHILE
for fruit in ["apple", "banana", "cherry"]:
print(fruit)
count = 0
while count < 3:
print(count)
count += 1
💡 The Unique Python for...else Clause
Python loops support an optional else: block! The else block executes only if the loop completed naturally without encountering a break statement:
# Finding primes with for...else
num = 17
for i in range(2, num):
if num % i == 0:
print(f"{num} is composite (divisible by {i})")
break
else:
# Runs ONLY if the loop ran to completion without hitting break!
print(f"{num} is prime!")
4.3 break, continue, and pass
Three small keywords give you fine control over loop and block behavior:
break: Exits the nearest enclosing loop immediately, skipping any remaining iterations entirely.continue: Is gentler — it skips only the rest of the current iteration and jumps straight to the next one, without abandoning the loop altogether.pass: Is unrelated to loops specifically; it is Python's official "do absolutely nothing" statement, used anywhere the language's grammar demands a statement but you don't have any code to write yet — for example, stubbing out an empty function body while you're still designing your program.
# Demonstrating break, continue, and pass
for n in range(1, 10):
if n == 3:
continue # Skip printing 3, jump to 4
if n == 7:
break # Exit loop completely when reaching 7
print(n, end=" ")
# Output: 1 2 4 5 6
# pass as an architectural placeholder:
def process_future_payment():
pass # Will implement payment gateway logic later!
💡 pass vs ... (Ellipsis) vs NotImplementedError
pass: No-op statement. Code continues execution normally....(Ellipsis): Python singleton object, often used in type hints and abstract class signatures.raise NotImplementedError: Deliberately crashes if a subclass fails to implement an abstract method. Preferred overpassin base classes!