Control Flow

Mastering if-else statements, loops (for, while), and understanding flow control.


  1. What is the purpose of an if-else statement in Python?

    age = 18
    if age >= 18:
        print("You are an adult.")
    else:
        print("You are a minor.")

2

How do you use elif to check multiple conditions?

score = 85
if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: D")

3

How can you use nested if-else statements?


4

What is the difference between break and continue in loops?


5

How do you create a while loop that terminates when a condition is met?


6

How can you use a for loop to iterate over a list?


7

What is the purpose of the else block in a loop?


8

How do you use a while loop with an else block?


9

How do you use the range function with a for loop?


10

How can you create an infinite while loop and terminate it using break?


11

How can you use for loops with dictionaries?


12

How do you use if statements to check for even and odd numbers?


13

How can you use while loops to perform an action until a condition is met?


14

How do you handle multiple conditions using logical operators in if statements?


15

How can you use else in an if-else chain to handle a default case?


16

How do you iterate over characters in a string using a for loop?


17

How can you use break to exit a for loop early?


18

How do you use continue to skip certain iterations in a loop?


19

How can you check for membership in a list using if statements?


20

How can you handle multiple loops with nested loops?


21

How can you use if statements to check if a number is positive, negative, or zero?


22

How do you use a for loop to iterate over a range with a specific step value?


23

How can you use a while loop to count down from a specified number and stop when a specific condition is met?


24

How can you use nested for loops to create a multiplication table?


25

How do you use list comprehensions with conditional statements?


Last updated