From chapter 10:
listis an ordered collection of items that can be of any type, e.g.,
["Joy and Beauty of Data", 2017].
- List creation, e.g.,
row = ["black", "blue", "orange"]
- Determining list length:
- Accessing list elements
- List memebership:
- List concatenation:
- List repetition:
- List slices
- Lists are mutable (unlike strings)
row = "yellow"
row[1:3] = 
row[2:2] = ["red", "magenta"]
- List aliasing: row_2 = row
- List cloning: row_2 = row[:]
- List methods:
- List append vs. list concatenation
- For loop list traversal
- by element
- by position
- Function parameters as lists
- Pure functions
- Functions returning lists
- List comprehensions
- Nested lists
- Creating lists from strings using
- Creating strings from lists using
Work the following exercises:
- Create a list called
myListwith the following six items: 76, 92.3, “hello”, True, 4, 76. Do it with both append and with concatenation, one item at a time.
- Starting with the list of the previous exercise, write Python statements to do the following:
- 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
- This page contains 2013 census information when the total population of the U.S. was 316,128,839.
- The file nested-lists.py contains information about the 10 most populous states.
- Add a 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
populationsvariable 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.