๐ List
5th Grade ๐ป Coding
A list is a single variable that holds many values in order, like a shopping list or a class roster. In Python, you write it in square brackets.
fruits = ["apple", "banana", "grape"] creates a list of three fruits. len(fruits) returns 3, fruits.append("mango") adds a fruit at the end, and fruits[0] is the first item, โapple,โ because counting starts at 0. A list can also hold numbers, and even other lists.
Lists and loops make a winning team: for f in fruits: print(f) goes through every item. With them you can find the average of test scores, search for a name, or sort numbers: sorted([5, 2, 9]) returns [2, 5, 9]. Scratch has lists too, in the Variables category.
For example
- Liamโs test scores:
[90, 85, 100], and you can calculate an average from them. - A game can keep all the enemies on the screen in a list and move each one every frame.
๐ Remember: A list = many values in a row, numbered from 0.
Quick quiz
What is a list in programming?
What does append do?
What does sorted([5, 2, 9]) return?