23.6 Exercises
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython4 class periods20 exercises20 graded tests
23.6 Exercises
Exercise 1
Write equivalent code using map instead of the manual accumulation below and assign it to the variable test.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
Use manual accumulation to define the lengths function below.
Open the
02folder and editmain.py. Run Tests checks it.
Exercise 3
Now define lengths using map instead.
Open the
03folder and editmain.py. Run Tests checks it.
Exercise 4
Now define lengths using a list comprehension instead.
Open the
04folder and editmain.py. Run Tests checks it.
Exercise 5
Write a function called positives_Acc that receives list of numbers as the input (like [3, -1, 5, 7]) and returns a list of only the positive numbers, [3, 5, 7], via manual accumulation.
Open the
05folder and editmain.py. Run Tests checks it.
Exercise 6
Write a function called positives_Fil that receives list of things as the input and returns a list of only the positive things, [3, 5, 7], using the filter function.
Open the
06folder and editmain.py. Run Tests checks it.
Exercise 7
Write a function called positives_Li_Com that receives list of things as the input and returns a list of only the positive things, [3, 5, 7], using the list comprehension.
Open the
07folder and editmain.py. Run Tests checks it.
Exercise 8
Define longwords using manual accumulation.
Open the
08folder and editmain.py. Run Tests checks it.
Exercise 9
Define longwords using filter.
Open the
09folder and editmain.py. Run Tests checks it.
Exercise 10
Define longwords using a list comprehension.
Open the
10folder and editmain.py. Run Tests checks it.
Exercise 11
Write a function called longlengths that returns the lengths of those strings that have at least 4 characters. Try it with a list comprehension.
Open the
11folder and editmain.py. Run Tests checks it.
Exercise 12
Write a function called longlengths that returns the lengths of those strings that have at least 4 characters. Try it using map and filter.
Open the
12folder and editmain.py. Run Tests checks it.
Exercise 13
Write a function that takes a list of numbers and returns the sum of the squares of all the numbers. Try it using an accumulator pattern.
Open the
13folder and editmain.py. Run Tests checks it.
Exercise 14
Write a function that takes a list of numbers and returns the sum of the squares of all the numbers. Try it using map and sum.
Open the
14folder and editmain.py. Run Tests checks it.
Exercise 15
Use the zip function to take the lists below and turn them into a list of tuples, with all the first items in the first tuple, etc.
Open the
15folder and editmain.py. Run Tests checks it.
Exercise 16
Use zip and map or a list comprehension to make a list consisting the maximum value for each position. For L1, L2, and L3, you would end up with a list [4, 5, 3, 5].
Open the
16folder and editmain.py. Run Tests checks it.
Exercise 17
Write code to assign to the variable compri_sample all the values of the key name in the dictionary tester if they are Juniors. Do this using list comprehension.
Open the
17folder and editmain.py. Run Tests checks it.
Exercise 18
Challenge The nested for loop given takes in a list of lists and combines the elements into a single list. Do the same thing using a list comprehension for the list L. Assign it to the variable result2.
Open the
18folder and editmain.py. Run Tests checks it.
Exercise 19
Challenge: Write code to assign to the variable class_sched all the values of the key important classes. Do this using list comprehension.
Open the
19folder and editmain.py. Run Tests checks it.
Exercise 20
Challenge: Below, we have provided a list of lists that contain numbers. Using list comprehension, create a new list threes that contains all the numbers from the original list that are divisible by 3. This can be accomplished in one line of code.
Open the
20folder and editmain.py. Run Tests checks it.
For teachers
23.6 Exercises
Students read Foundations of Python Programming's “Exercises” in CS Room and work through its 20 Python coding exercises (20 graded).
- Time: 4 class periods
- Tests: 20 in 20 graded exercises, set up for you when you import the lesson
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 1 |
03 | Autograded | 1 |
04 | Autograded | 1 |
05 | Autograded | 1 |
06 | Autograded | 1 |
07 | Autograded | 1 |
08 | Autograded | 1 |
09 | Autograded | 1 |
10 | Autograded | 1 |
11 | Autograded | 1 |
12 | Autograded | 1 |
13 | Autograded | 1 |
14 | Autograded | 1 |
15 | Autograded | 1 |
16 | Autograded | 1 |
17 | Autograded | 1 |
18 | Autograded | 1 |
19 | Autograded | 1 |
20 | Autograded | 1 |