Python Reading
- Read the chapter entitled Lists from section "Nested Lists" through section "Glossary".
Key Ideas
- Lists can be nested, e.g. [["Montana", 1132812 ], ["Wyoming", 584057]]
- A list can be created from a string
- declaration = "Four score and seven years ago"
- declaration.split()
- declaration.split("and")
- A string can be created from a list
- seuss = ["green", "eggs", "and", "ham"]
- glue = "*"
- glue.join(seuss)
- Sequences can be converted into lists, e.g. list("Bozeman Hawks")
- A tuple is like a list, except that it is immutable, e.g. today = ("Friday", "February", 23)
Active Learning
- This page contains 2023 census information when the total population of the U.S. was 334,914,895.
- The file nested-lists.py contains information about the 10 most populous states.
- Add a pure function to nested-lists.py that calculates and returns the total population of the information passed into it. Print this value after the function returns it.
- Modify the program to also print the percentage of the U.S. population that lives in the 10 most populated states.
- Modify the populations variable to include information about whether each state is landlocked. Consider a landlocked state to be one that does not touch the Atlantic Ocean, the Pacific Ocean or the Gulf of Mexico.
- Modify the program to also calculate and print the percentage of the population in the 10 most populated states that live in a landlocked state.
- Time permitting, make other interesting enhancements.