Page cover image

Control Flow

Control flow in Python refers to the order in which statements are executed within a program. Python provides several control flow structures that allow you to make decisions, repeat actions, and control the flow of your code. The primary control flow structures in Python include:

  • Conditional Statements (if, elif, else):

    • Conditional statements are used to make decisions in your code based on certain conditions.

    • The if statement allows you to execute a block of code if a condition is true.

    • The elif (short for "else if") statement is used to test multiple conditions if the preceding if condition is false.

    • The else statement defines what should happen if none of the preceding conditions are true.

    Example:

    x = 10 
    if x > 5: 
        print("x is greater than 5") 
    elif x == 5: 
        print("x is equal to 5")
    else: 
        print("x is less than 5")

  • Loops (for and while):

    • Loops allow you to repeat a block of code multiple times.

    • The for loop is typically used for iterating over sequences (e.g., lists, strings) or for a specified number of times.

    • The while loop continues executing a block of code as long as a specified condition is true.

    Example (for loop):

    fruits = ["apple", "banana", "cherry"] 
    for fruit in fruits: 
        print(fruit)

    Example (while loop):

    count = 0 
    while count < 5: 
        print(count) 
        count += 1

  • Break and Continue Statements:

    • The break statement is used to exit a loop prematurely, even if the loop condition is still true.

    • The continue statement is used to skip the rest of the current iteration of a loop and continue to the next iteration.

    Example (break):

    numbers = [1, 2, 3, 4, 5] 
    for number in numbers: 
        if number == 3: 
            break 
        print(number)

    Example (continue):

    numbers = [1, 2, 3, 4, 5] 
    for number in numbers: 
        if number == 3: 
            continue 
        print(number)

  • Comprehensions:

    • Comprehensions are concise ways to create lists, dictionaries, and sets using a single line of code. They often involve a for loop and an optional if condition.

    # Example - list comprehension:
    squares = [x ** 2 for x in range(1, 6)]
    
    # Example - list comprehension with if statement:
    even_squares = [num**2 for num in range(10) if num%2==0]
    
    # Example - list comprehension with if-else statement:
    even_squares = [num**2 if num%2==0 else 0 for num in range(10)]
    
    
    # Example - dictionary comprehension:
    fruit_lengths = {fruit: len(fruit) for fruit in fruits}

These control flow structures enable you to write flexible and dynamic code in Python, allowing you to handle various scenarios, make decisions, and perform repetitive tasks. Understanding how to use these constructs effectively is essential for writing efficient and readable Python programs.

Last updated

Was this helpful?