HTML

    Select a Subtopic

    Day 2: Control Flow in Python (if-else, loops)

    Topics to Cover:

    • Conditional Statements (if, else, elif)
    • Loops (for, while)
    • Special Statements: break, continue, pass

    1. Conditional Statements (if, else, elif)

    Conditional statements allow you to make decisions in your code based on certain conditions.

    1 2 3 4 5 6 7 8 age = int(input("Enter your age: ")) if age >= 18: print("You are eligible to vote!") elif age > 0: print("You are not old enough to vote.") else: print("Invalid age entered.")

    2. Loops (for, while)

    Loops help you repeat a block of code multiple times.

    For Loop:

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

    While Loop:

    1 2 3 4 count = 0 while count < 5: print("Count is:", count) count += 1

    3. Special Statements: break, continue, pass

    These statements help control the loop flow.

    StatementDescription
    breakStops the loop completely
    continueSkips the current iteration
    passDoes nothing (used as a placeholder)
    1 2 3 4 5 6 for number in range(10): if number == 5: break # Stops the loop when number is 5 if number % 2 == 0: continue # Skips even numbers print(number)

    Exercises:

    Exercise 1: Check Even or Odd

    Write a program that asks the user for a number and checks if it’s even or odd.

    1 # Your code here

    Exercise 2: Multiplication Table

    Write a program that prints the multiplication table of a number using a loop.

    1 # Your code here

    Exercise 3: Sum of Numbers from 1 to N

    Ask the user for a number N, then calculate and print the sum of numbers from 1 to N.

    1 # Your code here

    Exercise 4: FizzBuzz Challenge

    Print numbers from 1 to 100, but:

    • For multiples of 3, print "Fizz" instead of the number.
    • For multiples of 5, print "Buzz".
    • For multiples of both 3 and 5, print "FizzBuzz".
    1 # Your code here

    Challenge Yourself:

    Write a program that takes a number as input and prints prime numbers from 1 to that number.

    1 # Your code here