Lesson 14 of 60
โฎPrev Nextโฏ

PY-101 ยท Loops

for loops and range()

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

14of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A for loop runs a block once for each item in a sequence, such as a list, a string or a range of numbers. range(5) gives 0 to 4, and range(start, stop, step) lets you choose where to start, where to stop and how far to jump. The stop number is never included.

    • Loop over a list or string
    • Count with range()
    • Use start, stop and step

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    for loops and range(), running
    1. fruits = ["apple", "mango", "kiwi"]
    2. for fruit in fruits:
    3. print("I like", fruit)
    4. for n in range(1, 6, 2):
    5. print(n)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYou need to send 30 reminder emails. Writing 30 print lines is silly. A loop writes one and repeats it 30 times.
    2. The Python ideafor item in sequence:A for loop runs a block once for each item in a sequence, such as a list, a string or a range of numbers.
    3. In real lifeA payroll script loops over every employee and works out each salary. One loop, any number of employees.

    Start with a question

    You need to send 30 reminder emails. Writing 30 print lines is silly. A loop writes one and repeats it 30 times.

    Computers are great at repeating work. Every report, every list of results and every game frame uses loops.

    Explain it like I'm 10

    A loop is like a teacher calling the register: the same question for every name on the list, one at a time, until the list ends.

    The technical explanation

    for name in names: takes each item in turn and runs the indented block with name set to it.

    range(5) produces 0, 1, 2, 3, 4. range(2, 6) produces 2 to 5. range(0, 10, 2) counts in twos.

    A string is a sequence too: for ch in "hi": loops over each character.

    Syntax

    for item in sequence:
        ...
    for i in range(start, stop, step):
        ...

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    A payroll script loops over every employee and works out each salary. One loop, any number of employees.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does range(4) produce?
    2. 2. What does range(2, 10, 3) produce?
    3. 3. How many times does `for c in "cat":` run?
    4. 4. Is the stop value included in range?
    5. 5. Which prints 1 to 3?

    Questions people ask

    How do I loop with an index in Python?

    Use enumerate: for i, item in enumerate(items): gives you the position and the item together. It is clearer than range(len(items)).

    Can range count backwards?

    Yes, with a negative step: range(5, 0, -1) gives 5, 4, 3, 2, 1.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner How do I loop with an index in Python?

    Use enumerate: for i, item in enumerate(items): gives you the position and the item together. It is clearer than range(len(items)).

    beginner Can range count backwards?

    Yes, with a negative step: range(5, 0, -1) gives 5, 4, 3, 2, 1.

    intermediate What does range(4) produce?

    0, 1, 2, 3. It starts at 0 and stops before 4.

    intermediate What does range(2, 10, 3) produce?

    2, 5, 8. Start at 2, add 3 each time, stop before 10.

    Quick revision

    Remember

    • Loop over a list or string
    • Count with range()
    • Use start, stop and step
    • range(1, 10) stops at 9. If you want 1 to 10, write range(1, 11).

    Syntax

    for item in sequence:
        ...
    for i in range(start, stop, step):
        ...

    Official documentation