BBampaa

Lessons › Python

Python lesson

Lists

Python counts positions from 0, so sales[0] is the first item (96) and sales[2] is the third (175).

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

A list holds several values in order, inside square brackets. Each item has a position, counting from 0:

sales = [96, 120, 175]
print(sales)
print(sales[0])
print(sales[2])

Output:

[96, 120, 175]
96
175

Key points

[96, 120, 175]Square brackets, with commas between the items.
sales[0]The first item. Positions start at 0, not 1.
sales[-1]Negative positions count from the end: -1 is the last item.

Put together: products = ["Earbuds", "Charger"], then products[1] is "Charger".

Practice

Try each one in your head first, then open the answer.

1. Build a list called prices holding 35, 15 and 8.
prices = [35, 15, 8]
2. Build a line that shows the first item in prices.
print(prices[0])
3. Create a list called days holding "Mon", "Tue" and "Wed". Then print it.
days = ["Mon", "Tue", "Wed"]
print(days)
4. Print the second item in sales.
sales = [96, 120, 175, 120, 100]
print(sales[1])
5. Print the last item in sales.
sales = [96, 120, 175, 120, 100]
print(sales[-1])

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed