Save up to $499! Grab all Python courses for$49 or all online courses we’ve ever launched for only \$169. Only Feb 11-16. Happy Valentine's!
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])