Skip to content

Commit 914010a

Browse files
committed
Add some more exercises to lists and comprehensions
1 parent df26fee commit 914010a

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/05_lists.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,24 @@
2626
# YOUR CODE HERE
2727

2828
# Print all the values in x multiplied by 1000
29+
# YOUR CODE HERE
30+
31+
z = [
32+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
33+
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
34+
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
35+
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
36+
[40 ,41, 42, 43, 44, 45, 46, 47, 48, 49],
37+
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
38+
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
39+
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
40+
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
41+
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
42+
]
43+
44+
# Print the first element from each nested list in the matrix z
45+
# YOUR CODE HERE
46+
47+
# Print all of the elements that lie on the left-to-right
48+
# diagonal of matrix z
2949
# YOUR CODE HERE

src/08_comprehensions.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@
1717
# Write a list comprehension to produce the cubes of the numbers 0-9:
1818
# [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
1919

20-
y = []
20+
cubes = [i**3 for i in range(10)]
21+
22+
print(cubes)
23+
24+
# Write a list comprehension that utilizes slicing syntax to product
25+
# a list with the elements from the first half of the `cubes` list
2126

22-
print(y)
27+
first_half_of_cubes = [i for i in cubes[:5]]
28+
29+
print(first_half_of_cubes)
2330

2431
# Write a list comprehension to produce the uppercase version of all the
2532
# elements in array a. Hint: "foo".upper() is "FOO".
2633

27-
a = ["foo", "bar", "baz"]
34+
lowercase = ["foo", "bar", "baz"]
2835

29-
y = []
36+
uppercase = []
3037

31-
print(y)
38+
print(uppercase)
3239

3340
# Use a list comprehension to create a list containing only the _even_ elements
3441
# the user entered into list x.

0 commit comments

Comments
 (0)