for and range
Section 4: LoopsLesson 4.1 · ⏱ 8 min
for repeats the indented lines. And range(1, 6) gives the numbers from 1 to 5 (the last number is not included).
for i in range(1, 6):
print("Step", i)
print("Done!")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Step 1 Step 2 Step 3 Step 4 Step 5 Done!

📌 Another example: counting in steps
range(start, end, step): counting in steps of 5.
for i in range(0, 30, 5):
print(i)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗0 5 10 15 20 25
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Print the numbers from 1 to 10.
👀 Show the solution🙈 Hide the solution
for i in range(1, 11):
print(i)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗1 2 3 4 5 6 7 8 9 10
Result: 1 … 10
Medium
Count down from 10 to 1, then print Go!
👀 Show the solution🙈 Hide the solution
for i in range(10, 0, -1):
print(i)
print("Go!")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗10 9 8 7 6 5 4 3 2 1 Go!
Result: 10 … 1, then Go!
Hard
The sum of the numbers from 1 to 100.
👀 Show the solution🙈 Hide the solution
total = 0
for i in range(1, 101):
total = total + i
print(total)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗5050
Result: 5050

