انتقل للمحتوى
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

Going through the list

Section 5: ListsLesson 5.2 · ⏱ 7 min
scores = [7, 9, 4, 10]
for s in scores:
    print("Score:", s)
print("Best:", max(scores))
print("Total:", sum(scores))
🖥️ The output in Robo Python:
Score: 7
Score: 9
Score: 4
Score: 10
Best: 10
Total: 30
🐍 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 the list

We count how many numbers are bigger than 50.

🐍 Counting in the list
marks = [45, 80, 62, 30, 95]
count = 0
for m in marks:
    if m > 50:
        count = count + 1
print(count, "passed")
🖥️ The output in Robo Python:
3 passed
🐍 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 every name in the list with Hi.

👀 Show the solution
🐍 Solution
names = ["Ali", "Mona", "Omar"]
for n in names:
    print("Hi", n)
🖥️ The output in Robo Python:
Hi Ali
Hi Mona
Hi Omar
🐍 Open the code in Robo Python ↗

Result: Hi Ali, Hi Mona, Hi Omar

Medium

Print every word with its number of letters.

👀 Show the solution
🐍 Solution
words = ["sun", "moon", "star"]
for w in words:
    print(w, len(w))
🖥️ The output in Robo Python:
sun 3
moon 4
star 4
🐍 Open the code in Robo Python ↗

Result: sun 3, moon 4, star 4

Hard

A new list with double each number.

👀 Show the solution
🐍 Solution
nums = [1, 2, 3, 4]
doubled = []
for n in nums:
    doubled.append(n * 2)
print(doubled)
🖥️ The output in Robo Python:
[2, 4, 6, 8]
🐍 Open the code in Robo Python ↗

Result: [2, 4, 6, 8]

Finished the lesson? Mark it so your progress counts.