Skip to content

    Loops & Conditions

    Python · Beginner

    Definition

    if/elif/else chooses a branch; for and while repeat code.

    Control flow is the core of every program.

    Diagram

    if → yes / nofor item in listwhile cond

    Flowchart

    Check conditionRun matching branchLoop over items with forUse while until condition falsebreak/continue control loops

    Example: FizzBuzz

    for i in range(1, 6):
        if i % 15 == 0: print('FizzBuzz')
        elif i % 3 == 0: print('Fizz')
        elif i % 5 == 0: print('Buzz')
        else: print(i)

    Output: 1 2 Fizz 4 Buzz

    Key points

    • range(a, b) excludes b
    • enumerate gives index + value
    • for-else runs when no break

    Interview questions

    What does continue do?

    Skips the rest of the current iteration and moves to the next.

    More Python topics