List

Lists in Python represent ordered sequences of values. Here is an example of how to create them:

In [1]:

primes = [2, 3, 5, 7]

We can put other types of things in lists:

In [2]:

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']

We can even make a list of lists:

In [3]:

hands = [
    ['J', 'Q', 'K'],
    ['2', '2', '2'],
    ['6', 'A', 'K'], *# (Comma after the last element is optional)*]
*# (I could also have written this on one line, but it can get hard to read)*hands = [['J', 'Q', 'K'], ['2', '2', '2'], ['6', 'A', 'K']]

A list can contain a mix of different types of variables:

In [4]:

my_favourite_things = [32, 'raindrops on roses', help]
*# (Yes, Python's help function is *definitely* one of my favourite things)*

Indexing

You can access individual list elements with square brackets.

Which planet is closest to the sun? Python uses zero-based indexing, so the first element has index 0.

In [5]:

planets[0]

Out[5]: