header for databases

Python Fundamentals Exercises

With a data analysis spin

Complete the following exercises in a jupyter notebook with a descriptive name.

Set 1: Population change

Review this tool by the US Census bureau which displays data about US States, their counties, and their cities in a somewhat clunky interface.

The goal of this project is to create a python program using functions that can record data about the populations of US States and compute future population estimates based on growth percentages. US State population is the basis for determining electoral representation and is thus critical to the foundation of democracy.

  1. Declare an empty dictionary called state_populations. This variable will be "global" in that we'll declare it outside any function so it can be accessed by all our functions in every cell after the variable's declaration.
  2. Declare a function called input_state_population that takes in the number of states whose data you wish to enter. So if you call input_state_popluation(5), the function will ask for 5 states and their current population.
  3. Now write the guts: This function, when called, should loop a number of times equal to the number of states to input. On each loop: the function starts by printing out current state number. Then the function should ask the user for the name of a state using input(). Finally, ask the user for the population in that state for our current year. Store each of these values in local variables with appropriate names.
  4. Once the input_state_popluation has gathered both pieces of information from the user about a single state, the function should write the state's name as the key in your state_populations dictionary and the value should be the state's population as an integer.
  5. The function should continue asking for state name/population pairs until the requested number of states have been inputted.
  6. Declare a function called grow_state_populations that takes in a value between 0 and 1 that represents the decimal version of an annual percentage growth rate to be used to compute a future state population for each state inputted. So calling grow_state_populatoins(0.06) will mean: grow each state's population by 6%.
  7. Write the guts of our grow_state_populations function that reads our state_populations dictionary, one state at a time, and computes the new population size after one year of growth at the rate inputted into grow_state_populations. The results should be written to a new dictionary called state_population_growth whose keys are identical to state_populations but whose values are the original state populations + growth. Best to declare this dictionary on its own line outside the function just like state_populations so other functions can access it easily.
  8. The grow_state_populations function should conclude by printing out BOTH the state_populations dictionary and the new dictionary that contains the population values subjected to the desired growth rate.
  9. In the final cell of your program, call your input_state_popluation function, then call your grow_state_populations function, with the results appearing when you run that final cell.
  10. Extension A: Compute the total change in population across all states between your state_populations dictionary and your state_population_growth and displays the result to the user.
  11. Extension B: Adjust your grow_state_populations function to take in two parameters, the first being the same growth rate as the original exercise and the new, second parameter being the number of years (cycles) to apply the growth rate. So the new function, if called like this: grow_state_populations(.04, 6) will apply a 4% growth rate to each state's population for six years, compounding as it goes.
  12. Extension C: Adjust your program to write the results of both state population dictionaries to a file called statepop.txt. The first line of the file should state the growth rate to be applied to each state. The next X lines of the file should each contain a single state's data: first the state name, then a colon, then the original population value followed by a comma, and then the growth projection.

Sample program bits

sample code for first part of exercise