Control Flow Statements in Python β
ifif...elseif...elif...else
These are the primary structures for controlling the flow of execution based on conditions.
whileβ Used to repeat a block of code based on a condition.forβ Used to repeat a block of code a fixed number of times.elseβ Executes code when the preceding condition is not true.breakβ Exits the loop immediately.continueβ Skips the rest of the current loop iteration and moves to the next iteration.passβ Does nothing; used as a placeholder.assertβ Used for debugging purposes to test if a condition is true.returnβ Exits a function and optionally returns a value.
Note: Python does not have a switch case statement. but match case is introduced in python 3.10
Altering Control Flow β
if,elif,elseβ Conditional execution of code blocks based on boolean expressions.forloop β Used to repeat code a fixed number of times, often iterating over a sequence (like a list or range).whileloop β Repeats a block of code as long as a specified condition is true.
Conditional Execution β
Python allows you to track conditions efficiently with boolean expressions (True or False). Some examples include:
if m % n != 0: # This executes only if m % n != 0 is True
(m, n) = (n, m % n)Alternating Execution (using else):
if m % n != 0:
(m, n) = (n, m % n)
else:
gcd = n # Optional else branchIn Python, certain values are treated as False in boolean contexts:
- Numeric value
0 - Empty sequences:
"",[]
For example, the expression if m % n will be True if there is a remainder, and False if the remainder is 0.
Checking Membership β
You can check if a value is in or not in a sequence (like a list or string):
- Check if a value is in a list:
"Mushroom" in some_list- Check if a value is not in a list:
if user not in banned_users:
print(f"{user.title()}, you can post a response.")Types of if Statements β
- Simple
ifstatement β A single condition with one action when the condition is true:
if condition:
# actionif...elsestatement β Takes one action if the condition is true, and a different one if it's false:
if condition:
# action 1
else:
# action 2if...elif...elsechain β Used when more than two possible outcomes need to be considered. Python checks each condition in order, and once one condition is true, it skips the rest:
if condition1:
# action 1
elif condition2:
# action 2
else:
# action 3if...if...ifchain β Checks all conditions, even if one of them is true. Eachifis independent:
if condition1:
# action 1
if condition2:
# action 2
if condition3:
# action 3Multi-way Branching β
When you have multiple conditions to check, nested if statements can become hard to read. Hereβs an example with nested if statements:
if x == 1:
y = f1(x)
else:
if x == 2:
y = f2(x)
else:
if x == 3:
y = f3(x)
else:
y = f4(x)This is difficult to follow. A cleaner way is using elif to avoid unnecessary nesting:
if x == 1:
y = f1(x)
elif x == 2:
y = f2(x)
elif x == 3:
y = f3(x)
else:
y = f4(x)Using if Statements with Lists β
You can check for special values and ensure that a list is not empty. When the name of a list is used in an if statement, Python returns True if the list contains at least one item.
request = []
if request:
for _ in request:
print(f"Adding {_}.")
else:
print("You want something?")Using Multiple Lists: Checking and Comparing Two Lists β
You can compare two lists to check if items are present in both.
having = ["apple", "banana", "cherry"]
ordered = ["apple", "grape", "cherry"]
for item in ordered:
if item in having:
print(f"Adding {item}")
else:
print(f"Sorry, we don't have {item}")CS50 Conditionals β
Conditions allow you to take different paths based on boolean expressions (Yes or No).
Example 1: Basic Comparison β
x = int(input("What is x? "))
y = int(input("What is y? "))
if x < y:
print("x is less than y")
if x > y:
print("x is greater than y")
if x == y:
print("x is equal to y")Even if the first condition is true, Python will execute all the lines asking three questions.
Example 2: Using elif to Avoid Unnecessary Checks β
Using elif helps avoid executing unnecessary conditions after one is found to be true:
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")Using else with if β
The else statement is used to handle the case where none of the previous conditions were true.
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x is equal to y")Using or for Multiple Conditions β
The or operator can be used to combine multiple conditions into one line:
if x > y or x < y:
print("x is not equal to y")
else:
print("x is equal to y")Alternatively, you can use != to simplify the condition:
if x != y:
print("x is not equal to y")
else:
print("x is equal to y")Using Multiple if Statements β
When you use multiple if statements (instead of elif), each condition is checked independently, and all true conditions will result in executed code:
score = int(input("Score: "))
if score >= 90:
print("Grade: A")
if score >= 80:
print("Grade: B")
if score >= 70:
print("Grade: C")
else:
print("Grade: F")Checking for Even or Odd Numbers (Parity) β
You can use the modulus operator (%) to determine if a number is even or odd. An even number divided by 2 leaves a remainder of 0.
Example: β
if x % 2 == 0:
print("x is even")
else:
print("x is odd")Using Boolean Functions β
A boolean function returns True or False and can be used in if statements for conditions.
Example: β
def is_even(n):
if n % 2 == 0:
return True
else:
return False
def main():
x = int(input("What is x? "))
if is_even(x): # Calls the is_even function
print("Even")
else:
print("Odd")Alternatively, you can simplify the function to return the result directly:
def is_even(n):
return n % 2 == 0
def main():
x = int(input("What is x? "))
if is_even(x): # Directly checks if the result is True
print("Even")
else:
print("Odd")Simplifying Multiple if Conditions with or β
Instead of checking for multiple conditions with multiple if statements, you can combine conditions into one line using or:
name = input("What is your name? ")
if name == "Harry" or name == "Hermione" or name == "Ron":
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")