Python Interview Questions
Python
Web DevelopmentFrontendBackendData ScienceQuestion 11
What are the different control flow statements in Python?
Answer:
Control flow statements in Python allow you to control the execution of your code based on conditions, loops, and exceptions. Here are the different types of control flow statements available in Python:
Conditional Statements
-
ifStatement- Executes a block of code if a specified condition is true.
if condition: # Code to execute if condition is true -
if...elseStatement- Provides an alternative block of code to execute if the condition is false.
if condition: # Code to execute if condition is true else: # Code to execute if condition is false -
if...elif...elseStatement- Allows multiple conditions to be checked in sequence, executing the corresponding block of code for the first true condition.
if condition1: # Code to execute if condition1 is true elif condition2: # Code to execute if condition2 is true else: # Code to execute if none of the conditions are true
Looping Statements
-
forLoop- Iterates over a sequence (such as a list, tuple, string, or range) and executes a block of code for each item in the sequence.
for item in sequence: # Code to execute for each itemExample:
for i in range(5): print(i) # Output: 0 1 2 3 4 -
whileLoop- Repeatedly executes a block of code as long as a specified condition is true.
while condition: # Code to execute while condition is trueExample:
i = 0 while i < 5: print(i) i += 1 # Output: 0 1 2 3 4
Loop Control Statements
-
breakStatement- Terminates the loop prematurely, skipping any remaining iterations.
for i in range(10): if i == 5: break print(i) # Output: 0 1 2 3 4 -
continueStatement- Skips the current iteration and proceeds with the next iteration of the loop.
for i in range(10): if i % 2 == 0: continue print(i) # Output: 1 3 5 7 9 -
passStatement- A null operation; it does nothing and is used as a placeholder in situations where a statement is syntactically required but no action is needed.
for i in range(5): if i == 3: pass print(i) # Output: 0 1 2 3 4
Exception Handling Statements
-
try...exceptStatement- Allows you to handle exceptions (errors) that occur during execution of a block of code.
try: # Code that might raise an exception except ExceptionType: # Code to execute if an exception of type ExceptionType occursExample:
try: x = 1 / 0 except ZeroDivisionError: print("Cannot divide by zero") # Output: Cannot divide by zero -
try...except...elseStatement- Adds an optional
elseblock that executes if no exceptions are raised in thetryblock.
try: # Code that might raise an exception except ExceptionType: # Code to execute if an exception of type ExceptionType occurs else: # Code to execute if no exceptions occur - Adds an optional
-
try...except...finallyStatement- Adds a
finallyblock that executes regardless of whether an exception is raised or not.
try: # Code that might raise an exception except ExceptionType: # Code to execute if an exception of type ExceptionType occurs finally: # Code to execute regardless of whether an exception occursExample:
try: x = 1 / 1 except ZeroDivisionError: print("Cannot divide by zero") finally: print("This will always execute") # Output: This will always execute - Adds a
Summary
Control flow statements in Python allow you to manage the execution order and conditions under which code runs. They are essential for implementing logic, iterating over data, handling exceptions, and controlling the behavior of your programs. By mastering these statements, you can write more efficient, readable, and maintainable code.