Introduction
List basics
5. Accessing multiple item elements
Lists with if statements and loops
Lists in functions
Summary

Instruction

Good. We can also use square brackets to get multiple elements at once.

# define a list
companies = ['Royal Dutch Shell', 'BP', 'Total', 'Volkswagen', 'Glencore Xstrata', 'Gazprom']

# get top 3 companies
print(companies[0:3])

We used companies[0:3] to get all list elements with indices from 0 (inclusive) to 3 (exclusive). This means we got companies[0], companies[1], and companies[2] (but not companies[3]). The result is a new list.

Exercise

What was the water level during the weekend (i.e., days 6 and 7) in your previously defined list? Print two list elements.

Stuck? Here's a hint!

Use

print(water_level[5:7])