Control Flow in Python

Control flow statements in Python are used to control the flow of execution based on certain conditions.

Conditional Statements

Conditional statements like if, elif, and else are used to execute different blocks of code based on certain conditions.

if condition:
    # code block
elif condition:
    # code block
else:
    # code block

Loops

Loops like for and while are used to iterate over a sequence of elements or execute a block of code repeatedly until a condition is met.

for item in sequence:
    # code block

while condition:
    # code block

Break and Continue

The break statement is used to exit a loop prematurely, while the continue statement is used to skip the rest of the loop's code block and continue to the next iteration.

for item in sequence:
    if condition:
        break
    # code block

for item in sequence:
    if condition:
        continue
    # code block