Control Flow
Control Flow in Python refers to the order in which the statements and instructions in a Python program are executed. By default, Python executes code sequentially from top to bottom, but control flow structures allow you to make decisions, repeat actions, or change the execution path based on conditions. The main control flow tools in Python include:
Logical Expression
Logical Expression is a statement that can either be true or false. The following are some of the comparison operators:
1 > 2 is 1 bigger than 2 (Result is False)1 < 2 is 1 smaller than 2 (Result is True)1 == 2 is 1 equal to 2 (Result is False)1 != 2 is 1 not equal to 2 (Result is True)1 => 2 is 1 is equal or bigger than 2 (Result is False)1 <= 2 is 1 is equal or smaller than 2 (Result is True)
Example
if 100 > 99: # Condition: if 100 is greater than 99 (True)
print(“Good!”) # Since the condition is True, print “Good!”
if 100 > 99:
print("Good!")
Result
Good!
Logical Operators
Logical Operators are used to connect\combine multiple logical expressions. The following are some of the logical operators:
1 == 1 and 2 == 2 is True because both statement are True1 == 1 and 1 == 2 is False because only one statement is True1 == 1 or 1 == 2 is True one of the statements is True1 == 2 or 1 == 2 is False because neither statements is True
Example
if 100 > 99 and 99 == 99: # Both conditions must be True for the code inside to execute
print(“Good!”) # Both conditions are True, so “Good!” is printed
if 100 > 99 and 99 == 99:
print("Good!")
Result
Good!
if Statement
if keyword executes a block of code when a logical condition is met. if statement is one of the most well-known statement types. The if statement usually ends with :, and the block of code after the if a statement is indented.
Example
if 1 == 1: # Condition evaluates to True
print(“Good!”) # Since the condition is True, print “Good!”
if 1 == 1:
print("Good!")
Result
Good!
Or, you can do that without indentation
Example
if True: print(“Good!”) # Condition evaluates to True, since the condition is True, print “Good!”
if True:print("Good!")
Result
Good!
if … elif Statement
if … elif keywords are used to check for multiple logical conditions.
Example
temp_value = 1 # Assign the value 1 to the variable temp_value
if temp_value == 1: # If temp_value equals 1
print(“Value is 1”) # Print this message
elif temp_value == 2: # Otherwise, if temp_value equals 2
print(“Value is 2”) # Print this message
elif temp_value == 3: # Otherwise, if temp_value equals 3
print(“Value is 3”) # Print this message
temp_value = 1
if temp_value == 1:
print("Value is 1")
elif temp_value == 2:
print("Value is 2")
elif temp_value == 3:
print("Value is 3")
Result
Value is 1
Now, change temp_value to 2 and try again
Example
temp_value = 2 # Assign the value 2 to the variable temp_value
if temp_value == 1: # If temp_value equals 1
print(“Value is 1”) # Print this message
elif temp_value == 2: # Otherwise, if temp_value equals 2
print(“Value is 2”) # Print this message
elif temp_value == 3: # Otherwise, if temp_value equals 3
print(“Value is 3”) # Print this message
temp_value = 2
if temp_value == 1:
print("Value is 1")
elif temp_value == 2:
print("Value is 2")
elif temp_value == 3:
print("Value is 3")
Result
Value is 2
if … else Statement
if … else keywords are used to check for one logical condition, if not, then execute the block of code after the else statement
Example
if 1 == 2: # Condition is False
print(“Value is 1”) # This line is skipped because the condition is False
else: # Otherwise
print(“Value is 2”) # Since the 1 == 2 condition is False, this line is executed
if 1 == 2:
print("Value is 1")
else:
print("Value is 2")
Result
Value is 2
for Statement
for keyword is used to iterate over a sequence (list, tuple, set, dict or, string) and executes a block of code every iteration. The for statement usually ends with :, and the block of code after the for a statement is indented.
Example
for item in range(5): # range(5) generates numbers 0,1,2,3,4
print(item) # Print the current value of item in each iteration
for item in range(5):
print(item)
Result
0
1
2
3
4
Or, you can do that without indentation
Example
for item in range(5):print(item) # range(5) generates numbers 0,1,2,3,4, then print the current value of item in each iteration
for item in range(5):print(item)
Result
0
1
2
3
4
while statement
while keyword is used to repeat executing a block of code as long as a condition is met. The while statement usually ends with :, and the block of code after the while a statement is indented.
Example
counter = 0 # Initialize a counter variable to 0
while counter < 5: # Continue looping while counter is less than 5
print(counter, “is less than 5”) # Print the current value of counter and message
counter = counter + 1 # Increment counter by 1 in each iteration
counter = 0
while counter < 5:
print(counter, "is less than 5")
counter = counter + 1
Result
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
break statement
break keyword is used to break for or while loop
Example
while True: # Start an infinite loop
break # Immediately exit the loop
print(“Looping…”) # This line will never run because break exits the loop first
while True:
break
print("Looping...")
Result
Nested statements
You can nest statements (Remember to indent when you nest)
Example
for item in range(5): # Loop through numbers from 0 to 4
if item == 2: # Check if the current item equals 2
print(“Item”, item, “Found!”) # Print the item 2
print(“Breaking the loop”, item) # Print a message before breaking
break # Exit the loop immediately
for item in range(5):
if item == 2:
print("Item", item, "Found!")
print("Breaking the loop", item)
break
Result
Item 2 Found!
Breaking the loop 2