9.17 Exercises
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period5 exercises5 graded tests
9.17 Exercises
Read first in the book: 9.14 Accumulator Pattern Strategies, 9.15 Dont Mutate A List That You Are Iterating Through, 9.16 Summary.
Exercise 1
For each word in the list verbs, add an -ing ending. Overwrite the old list so that verbs has the same words with ing at the end of each one.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
In XYZ University, upper level math classes are numbered 300 and up. Upper level English classes are numbered 200 and up. Upper level Psychology classes are 400 and up. Create two lists, upper and lower. Assign each course in classes to the correct list, upper or lower. HINT: remember, you can convert some strings to different types!
Open the
02folder and editmain.py. Run Tests checks it.
Exercise 3
Starting with the list myList = [76, 92.3, 'hello', True, 4, 76], write Python statements to do the following:
a. Append "apple" and 76 to the list.
- Insert the value "cat" at position 3.
- Insert the value 99 at the start of the list.
- Find the index of "hello".
- Count the number of 76s in the list.
- Remove the first occurrence of 76 from the list.
- Remove True from the list using
popandindex.
Open the
03folder and editmain.py. This one is not graded, so check it yourself.
Exercise 4
The module keyword determines if a string is a keyword. e.g. keyword.iskeyword(s) where s is a string will return either True or False, depending on whether or not the string is a Python keyword. Import the keyword module and test to see whether each of the words in list test are keywords. Save the respective answers in a list, keyword_test.
Open the
04folder and editmain.py. Run Tests checks it.
Exercise 5
The string module provides sequences of various types of Python characters. It has an attribute called digits that produces the string '0123456789'. Import the module and assign this string to the variable nums. Below, we have provided a list of characters called chars. Using nums and chars, produce a list called is_num that consists of tuples. The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.
Open the
05folder and editmain.py. Run Tests checks it.
For teachers
9.17 Exercises
Students read Foundations of Python Programming's “Exercises” in CS Room and work through its 5 Python coding exercises (4 graded).
- Time: 1 class period
- Tests: 5 in 4 graded exercises, set up for you when you import the lesson
Reading first
- 9.14 Accumulator Pattern Strategies
- 9.15 Dont Mutate A List That You Are Iterating Through
- 9.16 Summary
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 2 |
03 | Not graded; students check it themselves | 0 |
04 | Autograded | 1 |
05 | Autograded | 1 |