Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 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

  1. if Statement

    • Executes a block of code if a specified condition is true.
    if condition:
        # Code to execute if condition is true
  2. if...else Statement

    • 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
  3. if...elif...else Statement

    • 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

  1. for Loop

    • 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 item

    Example:

    for i in range(5):
        print(i)
    # Output: 0 1 2 3 4
  2. while Loop

    • Repeatedly executes a block of code as long as a specified condition is true.
    while condition:
        # Code to execute while condition is true

    Example:

    i = 0
    while i < 5:
        print(i)
        i += 1
    # Output: 0 1 2 3 4

Loop Control Statements

  1. break Statement

    • 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
  2. continue Statement

    • 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
  3. pass Statement

    • 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

  1. try...except Statement

    • 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 occurs

    Example:

    try:
        x = 1 / 0
    except ZeroDivisionError:
        print("Cannot divide by zero")
    # Output: Cannot divide by zero
  2. try...except...else Statement

    • Adds an optional else block that executes if no exceptions are raised in the try block.
    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
  3. try...except...finally Statement

    • Adds a finally block 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 occurs

    Example:

    try:
        x = 1 / 1
    except ZeroDivisionError:
        print("Cannot divide by zero")
    finally:
        print("This will always execute")
    # Output: This will always execute

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.

Recent job openings