3. Chapter 3: Python Flow Control¶
3.1. Python if…else Statement¶
3.1.1. 1. Basic if Statement¶
Executes the block only when the condition evaluates to True:
age = 20
if age >= 18:
print("Eligible to vote")
3.1.2. 2. Using if…else Statement¶
Provides an alternative execution path when the condition is False:
temperature = 15
if temperature > 25:
print("It's warm outside")
else:
print("It's cold outside")
3.1.3. 3. Using if…elif…else Chains¶
Allows testing multiple conditions in sequence:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: D")
3.1.4. 4. Nested if Statements¶
An if block can exist inside another for complex logic:
username = "admin"
password = "1234"
if username == "admin":
if password == "1234":
print("Access granted")
else:
print("Incorrect password")
3.1.5. 5. Using Logical Operators in Conditions¶
Combine multiple conditions using and, or, and not:
age = 25
is_verified = True
if age > 18 and is_verified:
print("User can proceed")
3.1.6. 6. Short-Hand if Statement (Single Line)¶
Useful for minimal conditional checks:
x = 10
if x > 5: print("x is greater than 5")
3.1.7. 7. Short-Hand if…else (Ternary Expression)¶
Compact inline conditional assignment:
num = 7
result = "Even" if num % 2 == 0 else "Odd"
print(result) # Output: Odd
3.1.8. 8. Checking Multiple Values with in¶
Efficient alternative to chained OR conditions:
day = "Sunday"
if day in ["Saturday", "Sunday"]:
print("Weekend")
else:
print("Weekday")
3.1.9. 9. Comparing Strings in Conditions¶
Common technique to handle case-insensitive comparisons:
username = "Alice"
if username.lower() == "alice":
print("Welcome Alice")
3.1.10. 10. Using pass in if Statement¶
pass acts as a syntactic placeholder to avoid errors for empty blocks:
value = 5
if value > 0:
pass # Placeholder for future logic
print("Program continues...")
3.2. Python for Loop¶
3.2.1. 1. Basic for Loop with a Sequence¶
Iterates over each element in a sequence:
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)
3.2.2. 2. Using range() in for Loop¶
Generates numbers from 0 to 4 by default:
for i in range(5):
print(i)
3.2.3. 3. Custom Start and Step with range()¶
Format: range(start, stop, step)
for i in range(2, 10, 2):
print(i)
# Output: 2, 4, 6, 8
3.2.4. 4. Iterating Through a String¶
Processes each character individually:
word = "Python"
for char in word:
print(char)
3.2.5. 5. Using for with Index via enumerate()¶
Provides both index and value during iteration:
languages = ["Python", "Java", "C++"]
for index, lang in enumerate(languages):
print(index, lang)
3.2.6. 6. Nested for Loops¶
A loop inside another loop, commonly used for matrices and grids:
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
3.2.7. 7. Using break in for Loop¶
Terminates the loop when the condition is met:
for number in range(1, 10):
if number == 5:
break
print(number)
3.2.8. 8. Using continue in for Loop¶
Skips the current iteration without stopping the loop:
for number in range(1, 6):
if number == 3:
continue
print(number)
3.2.9. 9. for Loop with else Clause¶
The else block executes when the loop finishes normally (not broken):
for num in range(3):
print(num)
else:
print("Loop completed successfully")
3.2.10. 10. Iterating Over Dictionaries¶
Allows iteration over keys, values, or key–value pairs:
student = {"name": "Alice", "age": 22, "grade": "A"}
for key, value in student.items():
print(key, ":", value)
3.3. Python while Loop¶
3.3.1. 1. Basic while Loop¶
Executes repeatedly as long as the condition evaluates to True:
count = 1
while count <= 5:
print(count)
count += 1
3.3.2. 2. Decrementing Counter in while Loop¶
Commonly used for countdown logic:
number = 5
while number > 0:
print(number)
number -= 1
3.3.3. 3. Using while with User Input¶
Loop continues until a specific input condition is met:
user_input = ""
while user_input != "exit":
user_input = input("Type 'exit' to stop: ")
3.3.4. 4. Infinite while Loop¶
Creates an endless loop unless interrupted with break:
while True:
print("Running...")
3.3.5. 5. Breaking a while Loop¶
break terminates the loop immediately:
count = 0
while True:
if count == 3:
break
print(count)
count += 1
3.3.6. 6. Using continue in while Loop¶
Skips the current iteration and proceeds to the next cycle:
number = 0
while number < 5:
number += 1
if number == 3:
continue
print(number)
3.3.7. 7. while Loop with else Clause¶
The else block runs if the loop ends without encountering break:
x = 0
while x < 3:
print(x)
x += 1
else:
print("Loop completed normally")
3.3.8. 8. Simulating do-while Loop Behavior¶
Python does not have a native do-while, but this pattern replicates it:
count = 0
while True:
print("Executing at least once")
count += 1
if count == 3:
break
3.3.9. 9. Nested while Loops¶
A while loop inside another while loop:
i = 1
while i <= 3:
j = 1
while j <= 2:
print(f"i={i}, j={j}")
j += 1
i += 1
3.3.10. 10. Using while for Input Validation¶
Ensures user input meets required constraints before proceeding:
age = -1
while age < 0:
age = int(input("Enter a positive age: "))
print("Valid age entered:", age)
3.4. Python break and continue¶
3.4.1. 1. Using break to Exit a Loop Early¶
The loop stops immediately when the break statement is encountered:
for number in range(1, 10):
if number == 5:
break
print(number)
3.4.2. 2. Using continue to Skip an Iteration¶
The current iteration is skipped, and the loop proceeds with the next cycle:
for number in range(1, 6):
if number == 3:
continue
print(number)
3.4.3. 3. break in a while Loop¶
Demonstrates breaking out of an infinite loop:
count = 0
while True:
if count == 3:
break
print(count)
count += 1
3.4.4. 4. continue in a while Loop¶
Illustrates bypassing specific values:
num = 0
while num < 5:
num += 1
if num == 2:
continue
print(num)
3.4.5. 5. Using break in Nested Loops¶
break exits only the inner loop:
for i in range(3):
for j in range(5):
if j == 2:
break
print(f"i={i}, j={j}")
3.4.6. 6. Using Flag with break for Controlled Exit¶
Flag variables help manage complex exit logic:
found = False
for number in range(10):
if number == 7:
found = True
break
print("Found:", found)
3.4.7. 7. break + else Interaction¶
The else executes only if the loop does not terminate via break:
for n in range(5):
if n == 10:
break
else:
print("Loop completed without break")
3.4.8. 8. continue for Input Filtering¶
Useful for skipping unwanted values during iteration:
for char in "python123":
if char.isdigit():
continue
print(char)
3.4.9. 9. Breaking Based on User Input¶
Common pattern for controlled loop termination:
while True:
user_input = input("Enter 'q' to quit: ")
if user_input.lower() == 'q':
break
3.4.10. 10. Performance Use Case with continue¶
Prevents unnecessary processing for invalid data, improving loop efficiency:
numbers = [1, -2, 3, -4, 5]
for num in numbers:
if num < 0:
continue
print(num)
3.5. Python pass Statement¶
3.5.1. 1. Basic Usage of pass¶
The pass statement acts as a placeholder where a statement is syntactically required but no action is needed:
if True:
pass
print("Program continues running")
3.5.2. 2. Using pass in an Empty Function¶
Commonly used when defining function stubs during development:
def future_feature():
pass
3.5.3. 3. Using pass in a Class Definition¶
Allows declaration of a class structure without immediate implementation:
class User:
pass
3.5.4. 4. Using pass in a Loop¶
Maintains structural correctness without altering control flow:
for i in range(5):
if i == 3:
pass
else:
print(i)
3.5.5. 5. Using pass in Conditional Blocks¶
Useful when logic for a condition will be implemented later:
score = 85
if score > 90:
print("Excellent")
elif score > 80:
pass
else:
print("Needs Improvement")
3.5.6. 6. pass vs continue vs break¶
Key differences:
pass → Does nothing
continue → Skips iteration
break → Terminates loop
for i in range(5):
if i == 2:
pass # Does nothing
print(i)
3.5.7. 7. Using pass as Placeholder in Exception Handling¶
Temporarily suppresses exception handling during development (use cautiously in production):
try:
risky_operation()
except Exception:
pass
3.5.8. 8. Using pass in Abstract Method Design¶
Used when defining interface-like structures before concrete implementations:
class Shape:
def draw(self):
pass
3.5.9. 9. pass in Function Control Flow Planning¶
Helps structure logic flow before final code integration:
def validate_user(user):
if not user:
pass
else:
print("User is valid")
3.5.10. 10. Real-World Use Case: Iterative Development¶
Facilitates staged development in agile environments without breaking code execution:
def api_handler(request):
# Feature under construction
pass