Control statements are vital in programming as they allow you to control the flow of execution in your code. In Python, several control statements help you make decisions, loop through code blocks, and handle exceptions.
Conditional Statement(if, elif, else)
Conditional statements allow you to execute different blocks of code based on specific conditions.
The most common conditional statement in Python is the
age = 25 if age < 18: print("You are underage.") elif age >= 18 and age < 60: print("You are an adult.") else: print("You are a senior citizen.")
In the above example, the program checks the value of the
age variable and prints the corresponding message based on the condition.
Match and Switch Statement : python 3.10+
python 3.10+, Python has implemented a switch case feature called “structural pattern matching”.
number = int(input("Enter day ::")) match number: case 1: return "Sunday" case 2: return "Monday" case 3: return "Tuesday" case 4: return "Wednesday" case 5: return "Thursday" case 6: return "Friday" case 7: return "Saturday" case _: return "Week has only 7 days"
Looping Statements (for, while)
Looping statements enable you to execute a block of code repeatedly. Python offers two types of looping statements:
whileloop. Let's take a look at examples of both:
list = [1,2,3] for i in list: print(i,end = " ") #Output = 1 2 3
For loop using
range(start, stop, step) method.
start(optional): The starting value of the sequence (inclusive). If not specified, it defaults to 0.
stop(required): The ending value of the sequence (exclusive). The
range()function will generate numbers up to but excluding this value.
step(optional): The difference between each number in the sequence. If not specified, it defaults to 1.
list = list(range(1, 6)) print(my_list) # Output: [1, 2, 3, 4, 5] for i in range(6): print(i,end = " ") #output = 0, 1, 2, 3, 4, 5(this exclude 6) for i in range(1,6): print(i,end = " ") #output = 1, 2, 3, 4, 5 '''Reverse number''' '''In this statement starts from 5 to (0+1).Here, -1 is for reverse''' for i in range(5,0,-1): print(i,end = " ") #Output = 5, 4, 3, 2, 1
count = 0 while count < 10: print("Count:", count) count += 1
while loop continues executing the code block until the condition
count < 10 becomes
Break and Continue statements
Python provides control statements to alter the flow of loops. Two commonly used control statements are
continue. Let's see them in action:
fruits = ["apple", "banana", "cherry"] for fruit in fruits: if fruit == "banana": break print(fruit)
In this example, the
break statement is used to terminate the loop prematurely when the value of
fruit is "banana". The loop exits at that point.
numbers = [1, 2, 3, 4, 5] for num in numbers: if num % 2 == 0: continue print(num)
continue statement skips the remaining code in the loop for even numbers. It moves on to the next iteration and prints only the odd numbers.
Looping statement with else block
loop-elsestatement in Python is often confusingly named.
An alternative way to understand it is as a
nobreakstatement. This means that the
elseblock is executed only if the loop completes naturally without encountering a "break" statement.
Take into account the following implementation of the Sieve of Eratosthenes. This algorithm is widely recognized for its ability to find
'''Prime number from 1 to 20''' list =  number = 20 for n in range(2, number): for factor in list: if n % factor == 0: break else: # no break list.append(n) print(list)#output = [2, 3, 5, 7, 11, 13, 17, 19]