Lists

Let's talk about a new type of data that probably sounds pretty familiar - the list.

You probably already have a good idea of what a list is. Think about the lists you might use every day - a grocery list, or maybe a list of chores to do?

Just like all those everyday lists, a list in Python is just a collection of things:

>>> ["pizza", "fries", 3, 2.5, "donuts"]

Python knows this is a list because it's wrapped in what we call square brackets [ ].

In the list below, we're creating the variable fruit and giving it a value - a list containing several fruit names as strings.

>>> fruit = ["apple", "banana", "grape"]

This next one contains different types of numbers, assigned to the variable mynumbers:

>>> mynumbers = [3, 17, -4, 8.8, 1]

Guess what these will output (then try them out in the shell if you want to find out):

>>> type(fruit)

>>> type(mynumbers)

A list can contain different kinds of objects, such as strings, integers and floats:

>>> [43, "book", 11.5, "computer"]

A list can also contain as many or as few of those objects as you want. In fact, it can even be empty:

>>> []

[]

>>> type([])

<class 'list'>