range
¶We can store collections of data, using Python's type list
.
For example, here are the average gross annual earnings for anesthesiologists, ophthalmologists, cardiologists, pediatricians, and family doctors, respectively (from https://www.cma.ca/Assets/assetslibrary/document/en/advocacy/Anesthesiologye.pdf and similar sources on cma.ca).
The general form of a list is:
[expression1, expression2, ..., expressionN]
We can store the data in Python as follows:
gross_earnings = [361681, 740741, 396105, 284600, 249154]
We enclose the data in square brackets, and items of the list (also called elements) are separated by commas. We access an element using its index  the order of the element in the list, starting from 0.
gross_earnings[0]
gross_earnings[1]
gross_earnings[4]
Note that, since there are 5 elements in the list and we start counting from 0, we only have indices 0, 1, 2, 3, and 4. The following produces an error:
gross_earnings[5]
The expression gross_earnings[2]
gives a float
value, so you can use the expression gross_earnings[2]
anywhere that you can use a float. For example:
0.6 * (gross_earnings[2] + 500)
You can access an element of a list, by using a variable to represent the index:
i = 2
gross_earnings[i]
The variable name i
is commonly used when the variable represents an index.
As we did for objects of type str
, we can obtain the length of a list using Python's function len
.
len(gross_earnings)
The function call produced 5, because there are five elements in the list that gross_earnings
refers to.
Since the list has 5 elements, the valid indexes into the list are 0, 1, 2, 3, and 4. In general, the last index is equal to the length of the list minus one:
gross_earnings[len(gross_earnings)  1]
Here is another example of using len
to calculate the last index and to access the last element in the list:
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])
Alternatively, we can use negative indexes to access the elements of a list, with index 1 corresponding with the rightmost element:
print(L[1]) # same as L[len(L)1]
print(L[2]) # same as L[len(L)2]
There are several other functions that can take lists as arguments, including min
, max
, and sum
:
values = [5.4, 6, 3.2, 8.1, 11]
print(min(values))
print(max(values))
print(sum(values))
We can also use the operator in
to check whether an object is an item in a list:
heart_rates = [78, 85, 90, 100, 97]
85 in heart_rates
95 in heart_rates
for
loop over range
¶Python has a builtin function named range
that produces a sequence of numbers. Here is a fragment of the documentation for range
:
range(start, stop[, step]) > range object

 Return an object that produces a sequence of integers from start (inclusive)
 to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j1.
 start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
 These are exactly the valid indices for a list of 4 elements.
 When step is given, it specifies the increment (or decrement).
We can use a for
loop to iterate over the sequence produced by range
. The first number is the sequence is by default 0 and the sequence continues up to but not including the argument to range
:
for num in range(10):
print(num)
If we want to start at 1 and go up 11, we can pass two arguments:
for num in range(1, 11):
print(num)
We can also specify the step size, so let's use that to print even numbers from 10 to 20 inclusive:
for value in range(10, 21, 2):
print(value)
range
¶Use range
and a for
loop to print every 4th number from 0 to 200, inclusive
for
loop over list
¶Let's print out each item from gross_earnings
:
print(gross_earnings[0])
print(gross_earnings[1])
print(gross_earnings[2])
print(gross_earnings[3])
print(gross_earnings[4])
The code above works, but what if there are 100 or even 100,000 items in gross_earnings
? We'd have to call print
once for each of them!
The only thing that varies in each line of code above is the index. We can use a variable to represent the index and change the value of the variable so that it to refers to the values 0
, 1
, 2
, 3
, and 4
. Here is one approach for solving this, using range
and a for
loop:
for i in range(5):
print(gross_earnings[i])
We can make the code even more general and replace 5
with the length of the list, so that this code will work regardless of the number of items in gross_earnings
:
for i in range(len(gross_earnings)):
print(gross_earnings[i])
As an alternative, we can use a for
loop to iterate over the items in gross_earnings
:
for salary in gross_earnings:
print(salary)
In the code above, variable salary
first refers to gross_earnings[0]
, then gross_earnings[1]
, and so on, until the end of the list is reached.
Let's write code to print the items of gross_earnings
that are less than 300,000.
Here is an English description of our approach to this problem: for each item in gross_earnings, check whether it is less than 300000 and if so, print it. Now, here is the code:
gross_earnings = [361681, 740741, 396105, 284600, 249154]
for i in range(len(gross_earnings)):
if gross_earnings[i] < 300000:
print(gross_earnings[i])
Let's trace the code above using the Python Visualizer.
An alternative approach is to iterate over the list directly:
for salary in gross_earnings:
if salary < 300000:
print(salary)
We can also trace the code above using the Python Visualizer to see how it differs from the previous version.
Suppose we want to compute the sum of all the values in a list. We can use the numeric accumulator pattern that we used with strings.
Here's the idea: we'll set up a variable total
that initially refers to 0
, and we'll keep adding adding values to it.
Initially, total
will refer to 0.
Then we'll add gross_earnings[0]
to total
.
Next, we'll add gross_earnings[1]
to total
.
and so on ...
Finally, we'll add gross_earnings[len(gross_earnings)1]
to total
.
Here is how to express this in Python:
gross_earnings = [361681, 740741, 396105, 284600, 249154]
total = 0
for i in range(len(gross_earnings)):
total = total + gross_earnings[i]
print("At iteration", i, "total = ", total)
print("Final total:", total)
It isn't necessary to print out the intermediate values of total
, of course. We just did that for illustration purposes.
Once we know the sum of a list, computing the average is straight forward: all we need to do is divide the sum by the number of elements.
First, consider how you can get the number of items in a list, using one of Python's builtin functions.
Now, compute the average of the items in gross_earnings
:
gross_earnings = [361681, 740741, 396105, 284600, 249154]
Check your answer: 406456.2
Compute the average of gross_earnings
, but exclude any earnings that are outside the range from 250000 (inclusive) to 400000 (exclusive).
Check your answer: 347462.0
Consider a list of strings that are represent handedness of subjects in a study and looks something like this:
hands = ["left", "right", "right", "right", "left", "mixed", "right", ...]
The only elements in the list are the strings "right"
, "left"
, or "mixed"
.
Write a program that will work on any such list and print the percentage of subjects from each category of handedness. For example, if you were to run it on the list ["right", "left", "right", "right"]
, it should print the output:
Righthanded: 75 %
Lefthanded: 25 %
Mixedhanded: 0 %
Don’t worry about formatting the numbers. You might see 75.00000
as your result and that’s fine for now. Name your program handed.py.