Here are some exercises for you to try on your own. Solutions are available at http://pragprog.com/titles/gwpy3/practical-programming.
Variable kingdoms refers to the list [’Bacteria’, ’Protozoa’, ’Chromista’, ’Plantae’, ’Fungi’, ’Animalia’]. Using kingdoms and either slicing or indexing with positive indices, write expressions that produce the following:
Repeat the previous exercise using negative indices.
Variable appointments refers to the list [’9:00’, ’10:30’, ’14:00’, ’15:00’, ’15:30’]. An appointment is scheduled for 16:30, so ’16:30’ needs to be added to the list.
Using list method append, add ’16:30’ to the end of the list that appointments refers to.
Instead of using append, use the + operator to add ’16:30’ to the end of the list that appointments refers to.
You used two approaches to add ’16:30’ to the list. Which approach modified the list and which approach created a new list?
Variable ids refers to the list [4353, 2314, 2956, 3382, 9362, 3900]. Using list methods, do the following:
In this exercise, you’ll create a list and then answer questions about that list.
In this exercise, you’ll create a list and then answer questions about that list.
Complete the examples in the docstring and then write the body of the following function:
| def same_first_last(L: list) -> bool: |
| """Precondition: len(L) >= 2 |
| |
| Return True if and only if first item of the list is the same as the |
| last. |
| |
| >>> same_first_last([3, 4, 2, 8, 3]) |
| True |
| >>> same_first_last(['apple', 'banana', 'pear']) |
| |
| >>> same_first_last([4.0, 4.5]) |
| |
| """ |
Complete the examples in the docstring and then write the body of the following function:
| def is_longer(L1: list, L2: list) -> bool: |
| """Return True if and only if the length of L1 is longer than the length |
| of L2. |
| |
| >>> is_longer([1, 2, 3], [4, 5]) |
| True |
| >>> is_longer(['abcdef'], ['ab', 'cd', 'ef']) |
| |
| >>> is_longer(['a', 'b', 'c'], [1, 2, 3] |
| |
| """ |
Draw a memory model showing the effect of the following statements:
| values = [0, 1, 2] |
| values[1] = values |
Variable units refers to the nested list [[’km’, ’miles’, ’league’], [’kg’, ’pound’, ’stone’]]. Using units and either slicing or indexing with positive indices, write expressions that produce the following:
Repeat the previous exercise using negative indices.
Gray Whales Count nonprofit 501(c)(3) corporation for research and education: http://www.graywhalescount.org/gwc/GWC_REPORTS.html.