Chapter 5: Control Flow in Python

Hands-on exercises to practice conditional statements and loops

Chapter 5 Concepts

This chapter covers the control flow structures in Python programming:

If Statements Elif Statements Else Statements For Loops While Loops Break & Continue Nested Loops
Exercise 1

Basic If Statements

Create a program that checks if a number is positive, negative, or zero.

# Get a number from the user
number = float(input("Enter a number: "))

# Check if it's positive, negative, or zero
# and print an appropriate message
  • Use input() to get a number from the user
  • Convert the input to a float
  • Use if, elif, and else to check conditions
  • Print whether the number is positive, negative, or zero
  • Test with different values
  • Exercise 2

    Grade Calculator

    Create a program that converts a numerical score to a letter grade.

    # Get a score from the user
    score = float(input("Enter your score (0-100): "))

    # Convert to letter grade:
    # 90-100: A, 80-89: B, 70-79: C, 60-69: D, below 60: F
  • Get a score from the user
  • Use if-elif-else statements to determine the grade
  • Handle scores outside the 0-100 range with an error message
  • Print the corresponding letter grade
  • Test with edge cases (e.g., 89.5, 100, 0)
  • Exercise 3

    For Loop Practice

    Create a program that prints the multiplication table for a given number.

    # Get a number from the user
    num = int(input("Enter a number: "))

    # Use a for loop to print the multiplication table
    # from 1 to 10
  • Get a number from the user
  • Use a for loop with range(1, 11)
  • Print each multiplication step
  • Format the output clearly (e.g., "5 x 3 = 15")
  • Test with different numbers
  • Exercise 4

    While Loop Practice

    Create a number guessing game using a while loop.

    # Generate a random number between 1 and 100
    import random
    secret_number = random.randint(1, 100)

    # Use a while loop to let the user guess until correct
    # Provide hints (too high/too low)
  • Import the random module
  • Generate a random number between 1 and 100
  • Use a while loop for repeated guessing
  • Provide feedback for each guess (too high/too low)
  • Count the number of guesses
  • Congratulate the user when they guess correctly
  • Exercise 5

    Break and Continue

    Create a program that demonstrates the use of break and continue.

    # Print numbers from 1 to 20, but:
    # - Skip multiples of 3 (use continue)
    # - Stop when reaching 16 (use break)
  • Use a for loop to iterate from 1 to 20
  • Use continue to skip multiples of 3
  • Use break to stop the loop at 16
  • Print each number that isn't skipped
  • Explain what break and continue do in comments
  • Exercise 6

    Nested Loops

    Create a program that uses nested loops to print patterns.

    # Print a right-angled triangle pattern
    # *
    # **
    # ***
    # ****
    # *****

    # Use nested for loops
  • Use nested for loops (one inside another)
  • The outer loop should control the rows
  • The inner loop should control the stars in each row
  • Print a right-angled triangle pattern
  • Try creating different patterns (e.g., pyramid)
  • Exercise 7

    FizzBuzz

    Create the classic FizzBuzz program that prints numbers with replacements.

    # Print numbers from 1 to 100, but:
    # - For multiples of 3, print "Fizz"
    # - For multiples of 5, print "Buzz"
    # - For multiples of both 3 and 5, print "FizzBuzz"
  • Use a for loop to iterate from 1 to 100
  • Use if-elif-else statements to check multiples
  • Print numbers or words according to the rules
  • Test that 15 prints "FizzBuzz"
  • Ensure the output is formatted correctly
  • Exercise 8

    Prime Number Checker

    Create a program that checks if a number is prime.

    # Get a number from the user
    num = int(input("Enter a number: "))

    # Check if the number is prime
    # (divisible only by 1 and itself)
  • Get a number from the user
  • Check if the number is less than 2 (not prime)
  • Use a for loop to check divisibility from 2 to sqrt(num)
  • Use a boolean flag to track if the number is prime
  • Print an appropriate message with the result
  • Exercise 9

    List Processing with Loops

    Create a program that processes a list of numbers using loops.

    # Start with a list of numbers