Here are some exercises for you to try on your own:
1. Assign a list that contains the atomic numbers of the six alkaline earth metals—beryllium (4), magnesium (12), calcium (20), strontium (38), barium (56), and radium (88)—to a variable called alkaline_earth_metals.
2. Which index contains Radium's atomic number? Write the answer in two ways, one using a positive index and one using a negative index.
3. Which function tells you how many items there are in alkaline_ earth_metals?
4. Write code that returns the highest atomic number in alkaline_ earth_metals. (Hint: use one of the list methods from Figure 5.5, on page 87.)
5. What is the difference between print 'a' and print 'a',?
6. Write a for loop to print all the values in list half_lives from section 5.5, Slicing, on page 92, one per line.
7. Write a for loop to print all the values in list half_lives from Section 5.5, Slicing, on page 92, all on a single line.
8. consider the following statement, which creates a list of populations of countries in eastern Asia (china, DPR Korea, Hong Kong, Mongolia, Republic of Korea, and Taiwan), in millions: country_ populations = [1295, 23, 7, 3, 47, 21]. Write a for loop that adds up all the values and stores them in variable total. (Hint: give total an initial value of zero, and, inside the loop body, add the population of the current country to total.)
9. create a list of temperatures in degrees celsius with the values 25.2, 16.8, 31.4, 23.9, 28, 22.5, and 19.6, and assign it to a variable called temps.
10. using one of the list methods, sort temps in ascending order.
11. Using slicing, create two new lists, cool_temps and warm_temps, which contain the temperatures below and above 20 degrees celsius, respectively.
12. Using list arithmetic, recombine cool_temps and warm_temps in into a new list called temps_in_celsius.
13. Write a for loop to convert all the values from temps_in_celsius into Fahrenheit, and store the converted values in a new list temps_in_ fahrenheit. The list temps_in_celsius should remain unchanged.
14. create a nested list where each element of the outer list contains the atomic number and atomic weight for an alkaline earth metal. The values are beryllium (4 and 9.012), magnesium (12 and 24.305), calcium (20 and 40.078), strontium (38 and 87.62), barium (56 and 137.327), and radium (88 and 226). Assign the list to a variable alkaline_earth_metals.
15. Write a for loop to print all the values in alkaline_earth_metals, with the atomic number and atomic weight for each alkaline earth metal on a different line.
16. Write a for loop to create a new list called number_and_weight that contains the elements of alkaline_earth_metals in the same order but not nested.
17. Suppose the file alkaline_metals.txt contains this:
4 9.012 12 24.305 20 20.078 38 87.62 56 137.327 88 226
Write a for loop to read the contents of alkaline_metals.txt, and store it in a nested list with each element of the list contains the atomic number and atomic weight for an element. (Hint: use string.split.)
18. Draw a memory model showing the effect of the following statements:
19. The following function does not have a docstring or comments. Write enough of both to make it easy for the next person to understand what the function does, and how, and then compare your solution with those of at least two other people. How similar are they? Why do they differ?
def mystery_function(values): result = 
for i in range(len(values)): result.append([values[i]]) for j in range(1, len(values)):
result[-1].append(values[j][i]) return result
20. Section 5.2, Modifying Lists, on page 85 said that strings are immutable. Why might mutable strings be useful? Why do you think Python made them immutable?
21. What happens when you sort a list that contains a mix of numbers and strings, such as [1, 'a', 2, 'b']? Is this consistent with the rules given in Chapter 3, Strings, on page 39 and Chapter 6, Making Choices, on the next page for how comparison operators like < work on numbers and strings? Is this the "right" thing for Python to do, or would some other behavior be more useful?
Was this article helpful?