๐Ÿ“‹ 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.

Free ยท US Letter ยท no sign-up
Google Classroom Save to Pinterest Email me

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

  1. What is a list in programming?

  2. What does append do?

  3. What does sorted([5, 2, 9]) return?

๐Ÿ”Ž Ask Thinkapedia another question