انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

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:
Step 1
Step 2
Step 3
Step 4
Step 5
Done!
🐍 Open the code in Robo Python ↗
The example in Robo Python: the code on the right and what appeared on the left
The example in Robo Python: the code on the right and what appeared on the left

📌 Another example: counting in steps

range(start, end, step): counting in steps of 5.

🐍 Counting in steps
for i in range(0, 30, 5):
    print(i)
🖥️ The output in Robo Python:
0
5
10
15
20
25
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Print the numbers from 1 to 10.

👀 Show the solution
🐍 Solution
for i in range(1, 11):
    print(i)
🖥️ The output in Robo Python:
1
2
3
4
5
6
7
8
9
10
🐍 Open the code in Robo Python ↗

Result: 1 … 10

Medium

Count down from 10 to 1, then print Go!

👀 Show the solution
🐍 Solution
for i in range(10, 0, -1):
    print(i)
print("Go!")
🖥️ The output in Robo Python:
10
9
8
7
6
5
4
3
2
1
Go!
🐍 Open the code in Robo Python ↗

Result: 10 … 1, then Go!

Hard

The sum of the numbers from 1 to 100.

👀 Show the solution
🐍 Solution
total = 0
for i in range(1, 101):
    total = total + i
print(total)
🖥️ The output in Robo Python:
5050
🐍 Open the code in Robo Python ↗

Result: 5050

Finished the lesson? Mark it so your progress counts.