If we'd like to store more than one value in a single variable, we can use lists. For example, here are the average gross annual earnings for anesthesiologists, non-doctors, cardiologists, pediatricians, and family doctors, respectively (from https://www.cma.ca/Assets/assets-library/document/en/advocacy/Anesthesiology-e.pdf and similar sources on cma.ca)

361.681

62.15

396.105

284.600

249.154

We can store the data in Python as followd:

In [2]:

```
gross_earnings = [361.681, 62.15, 396.105, 284.600, 249.154]
```

*element* of the list is separated by commas. We access the elements by their *index* -- the order of the element in the list, **starting from 0**.

In [3]:

```
gross_earnings[0]
```

Out[3]:

In [4]:

```
gross_earnings[1]
```

Out[4]:

In [5]:

```
gross_earnings[4]
```

Out[5]:

In [6]:

```
gross_earnings[5]
```

**gross_earnings**.

**gross_earnings[2]** like any other value (like 5, or the variable **memory**). For example:

In [8]:

```
.6*(gross_earnings[2] + 500)
```

Out[8]:

In [9]:

```
gross_earnings[1] = gross_earnings[1]*10
print(gross_earnings[1])
```

You can access elements not just by typing in numbers, but also by using variables as indices:

In [11]:

```
i = 2
print(gross_earnings[i])
```

In [17]:

```
i = 3-2
print(gross_earnings[i])
```

There is a way to obtain the length of a list: we use **len()**:

In [16]:

```
len(gross_earnings)
```

Out[16]:

We got 5, because 5 is the number of elements in gross_earnings. How to get the last element using that? If we have 5 elements in total, that means that the indices: 0, 1, 2, 3, 4 go up to 4 = 5 - 1 (because we started from 0.)

That means that the last element of the list **L** (to pick a generic name will be **L[len(L)-1]**

In [19]:

```
L = [4, 5, 6]
print("Length of L:", len(L))
print("The index of the last element of L:", len(L)-1)
print("The last element of L:", L[len(L)-1])
```

**len(L)-1**. Instead, we can just write (but only inside the square brackets) -1:

In [20]:

```
print(L[-1]) #same as L[len(L)-1]
print(L[-2]) #same as L[len(L)-2]
```

Suppose we want to print out all the earnings in **gross_earnings**. In other words, we want to print **gross_earnings[0]**, then **gross_earnings[1]**, then **gross_earnings[2]** and so on, but we don't know the size of **gross_earnings** in advance.

In other words, we want to print out
**gross_earnings[0]**

**gross_earnings[1]**

**gross_earnings[2]**

**gross_earnings[3]**

**...**

**gross_earnings[len(gross_earnings)-1]**

Or in *other* words, we want to print out
**gross_earnings[i]** for i == 0

**gross_earnings[i]** for i == 1

**gross_earnings[i]** for i == 2

**gross_earnings[i]** for i == 3

**...**

**gross_earnings[]** for i == len(gross_earnings)-1

There is a way to do this in Python.

You can print out all the numbers from 0 to 4 (inclusive) as follows:

In [21]:

```
for i in range(5):
print(i)
```

What happens here is that the line **print(i)** is repeated for i = 0, i = 1, ..., i = (5-1), so we first print 0, then 1, ..., then 4.

Note that since we're starting from 0 and go up until 5-1, we repeat the line exactly 5 times.

This construct is called a for-loop.

In general,
for i in range(N):

will repeate the BLOCK N times, for i = 0, 1, 2, ..., N-1.

The BLOCK can be more than one line long, of course.

Let's use a for-loop to print out the list gross_earnings. We already have all the ingredients: we know how to get i to be 0, then 1, then 2, ..., then len(gross_earnings)-1, and we know how to get the i-th element of **gross_earnings**. We just need to combine those together.

In [24]:

```
for i in range(len(gross_earnings)):
print("Element", i, ":", gross_earnings[i])
```

Suppose we only want to print the elements of **gross_earnings** that are no smaller than 100 (for marketing purposes, say.) How to do that?

By now, we know how to print something conditionally:

In [25]:

```
hours_slept = 7
if hours_slept < 8:
print("PRESS SNOOZE BUTTON")
```

In [27]:

```
hours_slept = 9
if hours_slept < 8:
print("PRESS SNOOZE BUTTON") #Will not get executed for hours_slept == 9
#No output at all
```

Let's use the same idea, but inside the for-loop.

In [29]:

```
gross_earnings = [361.681, 62.15, 396.105, 284.600, 249.154]
for i in range(len(gross_earnings)):
if gross_earnings[i] >= 100.0:
print(gross_earnings[i])
```

Let's trace this.

In [30]:

```
from IPython.display import YouTubeVideo
YouTubeVideo("v4QQWFaMgJw")
```

Out[30]:

Suppose we want to compute the sum of all the values in a list. Here's the ides: we'll set up a variable **s**, and we'll keep adding adding elements to it.

So at first, s will be 0.
Then we'll add gross_earnings[0] to it, and s will become gross_earnings[0].

Then we'll add gross_earnings[1] to it, and s will become gross_earnings[0] + gross_earnings[1].

...

Then we'll add gross_earnings[len(gross_earnings)-1] to it, and s will become the sum of all the elements in gross earnings.

Here is how to express this in Python.

In [32]:

```
gross_earnings = [361.681, 62.15, 396.105, 284.600, 249.154]
s = 0
for i in range(len(gross_earnings)):
s = s + gross_earnings[i]
print("At iteration", i, "s = ", s)
print("Final s:", s)
```

In [33]:

```
gross_earnings = [361.681, 62.15, 396.105, 284.600, 249.154]
s = 0
for i in range(len(gross_earnings)):
s = s + gross_earnings[i]
print("At iteration", i, "s = ", s)
print("Final s:", s)
print("Average:", s/len(gross_earnings))
```

(We just added one more line to the program that computed the sum.)

The first step here is to compute the sum of all the earnings between 100 and 300 in gross_earnings. We already did something very similar to this when we only printed out earnings that were at or above 100. Let's use the same trick here.

In [6]:

```
gross_earnings = [361.681, 62.15, 396.105, 284.600, 249.154]
s = 0
for i in range(len(gross_earnings)):
if gross_earnings[i] > 300:
print("i =", i, ". gross_earnings[i] = ", gross_earnings[i], ". Too high, excluded")
elif gross_earnings[i] >= 100:
print("i =", i, ". gross_earnings[i] = ", gross_earnings[i])
s = s + gross_earnings[i]
else:
print("i =", i, ". gross_earnings[i] = ", gross_earnings[i], ". Too low, excluded")
print("The sum of earnings in the 100.300 range is", s)
```

So far, so good, but we want the *average*. That means that we need to figure out how many elements we have added into **s**. Here's an idea for how to do this: in addition to the variable **s**, have another variable, called **count**. We will add 1 to **count** every time we add an element into **s**.

Let's explore this idea more, and come up with a program that counts the number of elements in a list that are within a certain range.

In [39]:

```
from IPython.display import YouTubeVideo
YouTubeVideo("mLGGnTLc9rI")
```

Out[39]:

Note that in the video, I forgot to set count to 0 in the beginning! For reference, the code is

In [40]:

```
L = [5, 6, 10, 2, 7]
count = 0
for i in range(len(L)):
if 5 <= L[i] <= 8:
count = count + 1
print("The number of elements of L that are in the range 5..8:", count)
```

**s**, we will be able to look at **count** and know exactly how many elements we've added into **s**, since every time we've added an element into **s**, we've increased **count** by one, so **count** will be exactly as large as the number of elements added into **s**!

In [7]:

```
gross_earnings = [361.681, 62.15, 396.105, 284.600, 249.154]
s = 0
count = 0
for i in range(len(gross_earnings)):
if gross_earnings[i] > 300:
print(gross_earnings[i], ": Too high, excluded")
elif gross_earnings[i] >= 100:
s = s + gross_earnings[i]
count = count + 1
else:
print(gross_earnings[i], ": Too low, excluded")
print("The sum of earnings in the 100..300 range is", s)
print("The number of elements added into s is", count)
print("The average of earnings in the 100..300 range is", s/count)
```

In [8]:

```
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
```