To do this exercise, download this notebook and open it on your own computer. There are several tasks described below. Your job is to change the code in the cells so that the output from running the cell matches the expected output indicated above the cell.
There is a list of numbers assigned to a variable number_list
in the code below that has been assigned a list of numbers. Run this cell before filling in the answers below.
number_list = [-2, -58, 4, 36, -6, 60, -57]
Write another expression in the cell below, using square bracket index notation, that causes the 4th element of number_list
(i.e., the number 36
) to be displayed when running the code in the cell.
Expected output: 36
Now write an expression that evaluates to the number of items in the list (i.e., 7
), using the len()
function.
Expected output: 7
The following expression:
print max(number_list)
... will print the largest value in number_list
(i.e., 60
). Change the variable x
in the code below so that the expression
print sorted(number_list)[x]
... does the same thing. (i.e., when you run the cell, it should display 60
.)
Expected output: 20
x = 0
sorted(number_list)[x]
-58
Write an expression below that evaluates to a slice of number_list
starting with its second element and ending with its fifth element (exclusive).
Expected output: [-2, -58, 4, 36, -6, 60, -57]
Now, write an expression below that evaluates to a slice of number_list
starting with its third element and ending at the end of the list.
Expected output: [4, 36, -6, 60, -57]
Finally, fill in a value for the variable x
below so that the expression below it evaluates to a slice of number_list
starting at the second-to-last element of the list and ending at the end of the list. (Hint: x
should be a negative integer.)
Expected output: [60, -57]
x = 0
number_list[x:]
[-2, -58, 4, 36, -6, 60, -57]
For this problem set, I'm introducing a new Python operator: the modulo operator, %
. This operator returns the remainder of dividing one integer by another. For example:
22 % 3
1
This expression evaluates to 1
because the remainder of dividing 22
by 3
is 1
. We can use the modulo operator to test whether or not a number is even (i.e., divisible by 2), by using the number 2
on the right side of the operator:
100 % 2
0
101 % 2
1
Given the above information, write a list comprehension that evaluates to a list containing only the members of number_list
that are divisible by three. Use the modulo operator in the membership expression of the list comprehension.
Expected output: [36, -6, 60, -57]
In the cell below, a variable float_str
is set to a string containing a list of floating-point numbers, separated by semicolons (;
). (Make sure to run this cell before you proceed, so that the variable will be available in subsequent cells.)
float_str = "5.8;6.9;3.1;5.9;6.6;6.5;6.5;5.6;6;6.4;3.32;6.0;6.0;6.3;6.6;6.6"
Write an expression below that converts this string into a list of floating-point numbers. The type of the expression should be list
and the type of individual elements in the list should be float
. Hint: You'll need to use the float()
function, the .split()
method, and a list comprehension.
Expected output:
[5.8, 6.9, 3.1, 5.9, 6.6, 6.5, 6.5, 5.6, 6.0, 6.4, 3.32, 6.0, 6.0, 6.3, 6.6, 6.6]
Using the expression you wrote above as a starting point, write an expression below that evaluates to the sum of the numbers in the list.
Expected output: 94.11999999999999
(or close to that)
In the cell below, I've defined a list of strings and assigned it to a variable called greek
. Make sure to run this cell before you continue.
greek = ["alpha", "beta", "gamma", "delta", "epsilon"]
Okay. Your job in the next cell is to write an expression that evaluates to a list of strings in greek
that contain exactly five letters.
Expected output: ["alpha", "gamma", "delta"]
In the cell below, write an expression that evaluates to a string containing each of the first letters of each string in greek
. Hint: You'll need to use a list comprehension and the .join()
method.
Expected output: 'abgde'
The cell below has the skeleton of the for
loop written for you. Replace the string "blip"
with an expression such that the cell, when executed, outputs the first ten multiples of five.
Expected output:
0
5
10
15
20
25
30
35
40
45
for i in range(10):
print("blip")
blip blip blip blip blip blip blip blip blip blip
Using the variable greek
that was defined earlier, write a for
loop in the cell below so that when the cell is executed it outputs, on separate lines, each of the elements of the list in upper case.
Expected output:
ALPHA
BETA
GAMMA
DELTA
EPSILON
You're done! Good job.