PYTHON LESSON 10 OF 32

Repeat with for loops

A for loop visits each item in a collection. range(3) produces 0, 1 and 2: the stop value is excluded. Keep repeated work indented under the loop.

हिन्दी में समझें

for लूप हर मान पर काम करता है। range(3) में 0, 1, 2 आते हैं; अंतिम सीमा शामिल नहीं होती। दोहराए जाने वाले निर्देश अंदर लिखें।

अभ्यास: range बदलें ताकि लूप 0 से 4 तक प्रिंट करे।

Hinglish mein samjhein

for loop har item par kaam karta hai. range(3) se 0, 1, 2 milte hain; 3 include nahi hota. Repeat wala code indent karo.

Practice: range badlo taaki loop 0 se 4 tak print kare.

A small Python example

for number in range(3):
    print(number)

The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.

Try this example in the Python playground →

Practise this idea

Change range so the loop prints 0 through 4.

Check answer & explanation
for number in range(5):
    print(number)

range(5) yields 0, 1, 2, 3 and 4. The stopping value 5 is excluded.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

How many times does this loop run?

  1. 2
  2. 3
  3. 4
Check the quiz answer

3

range(3) contains three values: 0, 1, 2.

References & corrections

Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.