Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
List basics
2. Defining lists
Lists with if statements and loops
Lists in functions
Summary

Instruction

Good. We already know some basic data types: integers, floats, and strings. Now, we want to analyze multiple values – for instance, the top six European companies by revenue. You could start by defining six separate variables like this:

company_1 = 'Royal Dutch Shell'
company_2 = 'BP'
company_3 = 'Total'
...

But that's a lot of typing. Besides, it's difficult to handle groups of data stored in this way. Python offers a few ways to store multiple values in a single variable. The most versatile one is called a list. Take a look:

companies = ['Royal Dutch Shell', 'BP', 'Total', 'Volkswagen', 'Glencore Xstrata', 'Gazprom']

A list begins and ends with a bracket. Inside these brackets are comma-separated elements. Because our elements are strings, we also need to surround them with quotes.

Exercise

Define a list variable named water_level with the following seven values that represent the daily water level in cm in an imaginary lake: 730, 709, 682, 712, 733, 751, 740. Print the list.

Stuck? Here's a hint!

Use the assignment operator (=) to create the water_level variable. Inside a pair of brackets, put all seven values separated with commas. Since these are integers, you should not use quotes.