In programming, it is common to want to work with collections of data. In Python, a list is one of the many built-in data structures that allows us to work with a collection of data in sequential order.
Suppose we want to make a list of the heights of students in a class:
- Noelle is 61 inches tall
- Ava is 70 inches tall
- Sam is 67 inches tall
- Mia is 64 inches tall
In Python, we can create a variable called
heights to store these integers into a list:
heights = [61, 70, 67, 64]
- A list begins and ends with square brackets (
- Each item (i.e.,
70) is separated by a comma (
- It’s considered good practice to insert a space (
) after each comma, but your code will run just fine if you forget the space.
Let’s write our own list!
Examine the existing list
heights in your code editor.
A new student just joined the class!
- Chloe is 65 inches tall
Add Chloe’s height to the end of the list
# in front of the definition of the list
broken_heights. If you run this code, you’ll get an error in your terminal:
SyntaxError: invalid syntax
Add commas (
broken_heights so that it runs without errors.