Learn
Creating and Modifying a List in Python
What is a list?

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]

Notice that:

  1. A list begins and ends with square brackets ([ and ]).
  2. Each item (i.e., 67 or 70) is separated by a comma (,)
  3. It’s considered good practice to insert a space () after each comma, but your code will run just fine if you forget the space.

Instructions

1.

A new student just joined the class:

  • Cole is 65 inches tall

Add Cole’s height to the end of the list heights.

2.

Remove the # 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 (,) to broken_heights so that it runs without errors.

Folder Icon

Sign up to start coding

Already have an account?