A list is an ordered set of objects in Python.
Suppose we want to make a list of the heights of students in a class:
- Jenny is 61 inches tall
- Alexus is 70 inches tall
- Sam is 67 inches tall
- Grace is 64 inches tall
In Python, we can create a variable called
heights to store these numbers:
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.
A new student just joined the class:
- Cole is 65 inches tall
Add Cole’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:
SyntaxError: invalid syntax
Add commas (
broken_heights so that it runs without errors.