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

What is a list?

Section 5: ListsLesson 5.1 · ⏱ 7 min

A list is a variable that keeps many things in order, between square brackets [ ]. Counting starts from zero.

robots = ["Robo", "Bolt", "Nova"]
print(robots[0])
print(len(robots))
robots.append("Zip")
print(robots)
🖥️ The output in Robo Python:
Robo
3
['Robo', 'Bolt', 'Nova', 'Zip']
🐍 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: add and remove

append adds at the end, and remove deletes.

🐍 Add and remove
pets = ["cat", "dog"]
pets.append("fish")
pets.remove("dog")
print(pets)
print(len(pets))
🖥️ The output in Robo Python:
['cat', 'fish']
2
🐍 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

A list of three colours, and print the first and the last.

👀 Show the solution
🐍 Solution
colors = ["red", "green", "blue"]
print(colors[0])
print(colors[-1])
🖥️ The output in Robo Python:
red
blue
🐍 Open the code in Robo Python ↗

Result: red, then blue

Medium

Sort a list of numbers from the smallest to the biggest.

👀 Show the solution
🐍 Solution
nums = [5, 2, 9, 1]
nums.sort()
print(nums)
🖥️ The output in Robo Python:
[1, 2, 5, 9]
🐍 Open the code in Robo Python ↗

Result: [1, 2, 5, 9]

Hard

The biggest number, the smallest number and the sum of the list.

👀 Show the solution
🐍 Solution
nums = [12, 7, 30, 4]
print(max(nums), min(nums), sum(nums))
🖥️ The output in Robo Python:
30 4 53
🐍 Open the code in Robo Python ↗

Result: 30 4 53

Finished the lesson? Mark it so your progress counts.