Home › Python with Chai › Lesson 15
range(): 10 cups at once
Python with Chai, lesson 15 of 20. A short Hinglish video, simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
range(1, 11) is a counting machine: it gives 1, 2, 3 … 10. Put it in a for loop and 2 lines do the work of 10.
The stop number is never included: range(5) gives 0 to 4, and range(1, 6) gives 1 to 5.
A third number is the step: range(2, 11, 2) jumps by 2 and gives 2, 4, 6, 8, 10.
The code
for i in range(1, 11):
print(f"Cup {i} ready")
print(list(range(5)))
print(list(range(2, 11, 2)))
Output:
Cup 1 ready Cup 2 ready Cup 3 ready Cup 4 ready Cup 5 ready Cup 6 ready Cup 7 ready Cup 8 ready Cup 9 ready Cup 10 ready [0, 1, 2, 3, 4] [2, 4, 6, 8, 10]
Try it yourself
- Print the 5 times table: 5, 10, 15 … 50 (hint: a step of 5).
- Count down from 10 to 1 with range(10, 0, -1).
Remember
range(start, stop, step): the stop never comes.
← f-strings: a neat bill · Default values: less sugar →
All lessons: Python with Chai