Python Tutorial 0/52 lessons ~6 min read Lesson 10

    Loops (for / while)

    Python's for loop iterates over any iterable (list, dict, string, file, generator).

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Python's for loop iterates over any iterable (list, dict, string, file, generator). while loops repeat until a condition is false.

    Understanding the topic

    Core concepts to understand:

    • for x in iterable: — most common.
    • range(n) for index loops.
    • enumerate() for index + value.
    • break, continue, else on loops.

    Syntax reference

    Visual flow / code:

    python
    # Iterate
    for fruit in ["apple", "banana"]:
    print(fruit)
    # Index + value
    for i, name in enumerate(["a", "b"], start=1):
    print(i, name)
    # Parallel iteration
    for k, v in zip(["a", "b"], [1, 2]):
    print(k, v)
    # while
    n = 5
    while n > 0:
    n -= 1

    Execution workflow

    1Loops (for / while) Workflow
    1 / 4

    for x in iterable

    — most common.

    Real-world use

    Pythonic loops avoid C-style index loops. Use enumerate, zip, and comprehensions instead of for i in range(len(x)).

    Best practices

    • Iterate directly, not by index.
    • Use enumerate when you need the index.
    • Use zip for parallel iteration.

    Common mistakes

    • Modifying a list while iterating it — make a copy first.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionfor-else clause — what does it do?+

    Answer

    The else block of a for or while loop runs only if the loop finishes without hitting break. It is useful for search loops: if you break when you find an item, the else handles the 'not found' case without a flag variable. It also runs if the iterable is empty, and continue does not prevent it.

    Follow-up

    Does else run when the loop body raises an exception?
    2BeginnerQuestionenumerate vs range(len)?+

    Answer

    enumerate(xs) yields (index, item) pairs lazily and works with any iterable, including generators. range(len(xs)) requires a sized sequence and forces you to index back in with xs[i], which is more verbose and error-prone. Prefer enumerate, and use its start argument if you want counting from 1.
    3BeginnerQuestionWhen use <code>while</code>?+

    Answer

    Use while when the number of iterations is not known in advance and depends on a condition, such as reading until input is exhausted, retrying until success, or polling. For iterating over a known collection or range, for is clearer and safer. A while loop needs you to update the condition yourself or you risk an infinite loop.

    Follow-up

    How do you write an intentional infinite loop and exit it?

    Summary

    In summary: Iterate directly. enumerate + zip are everyday tools.

    Ready to mark this lesson complete?Track your journey across the entire course.