23.6 Exercises

From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython4 class periods20 exercises20 graded tests

README.md

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 01 folder and edit main.py. Run Tests checks it.

Exercise 2

Use manual accumulation to define the lengths function below.

Open the 02 folder and edit main.py. Run Tests checks it.

Exercise 3

Now define lengths using map instead.

Open the 03 folder and edit main.py. Run Tests checks it.

Exercise 4

Now define lengths using a list comprehension instead.

Open the 04 folder and edit main.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 05 folder and edit main.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 06 folder and edit main.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 07 folder and edit main.py. Run Tests checks it.

Exercise 8

Define longwords using manual accumulation.

Open the 08 folder and edit main.py. Run Tests checks it.

Exercise 9

Define longwords using filter.

Open the 09 folder and edit main.py. Run Tests checks it.

Exercise 10

Define longwords using a list comprehension.

Open the 10 folder and edit main.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 11 folder and edit main.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 12 folder and edit main.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 13 folder and edit main.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 14 folder and edit main.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 15 folder and edit main.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 16 folder and edit main.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 17 folder and edit main.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 18 folder and edit main.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 19 folder and edit main.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 20 folder and edit main.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

FolderGradingTests
01Autograded1
02Autograded1
03Autograded1
04Autograded1
05Autograded1
06Autograded1
07Autograded1
08Autograded1
09Autograded1
10Autograded1
11Autograded1
12Autograded1
13Autograded1
14Autograded1
15Autograded1
16Autograded1
17Autograded1
18Autograded1
19Autograded1
20Autograded1