Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lab Exercise #7 Assignment Overview This lab exercise provides practice with lists, functions and namespaces in Python. You will work with a partner on this
Lab Exercise #7 Assignment Overview This lab exercise provides practice with lists, functions and namespaces in Python. You will work with a partner on this exercise during your lab session. Two people should work at one computer. Occasionally switch the person who is typing. Talk to each other about what you are doing and why so that both of you understand each step Immigration Immigration, especially illegal immigration, is a perennial, hot topic so in this lab we will take a look at some data. We use data from the Pew Research Center-data on illegal immigration is an estimate so its source is important.1 Pew has published trends covering decades such as the following figure, but we will examine one data point: 2016 (the latest year of good data). We downloaded a spreadsheet from Pew Research for 2016 data on each state as well as a summary for the US. Each state is a row in the spreadsheet. We provide the spreadsheet as a CSV (comma separated value) file named immigration.csv Open the file in Excel (or a similar program) to see what we are providing. Pew uses the term "unauthorized" for what most people call "illegal." We are interested in answering three questions: 1. If we add up the individual state unauthorized immigration population (at index 1), do we get the same value as in the summative row labeled "U.S."? (Food for thought: why aren't they the same Which states have a larger percentage unauthorized immigrant population (at index 2) than the value in the summative row labeled "U.S."? Consider the column on the industry with the largest number of unauthorized immigrant workers (at index 9). Which industry is listed as the largest in the most states and how many states? 2. 3. We provide a skeleton program with a main () and four function headers, one function to read the file plus one function for each question. 1. read_file (fp): this function takes a file pointer as an argument and returns a list of def lists. The list of lists where each list is a list of the contents of each data row of the file ignoring header rows, footnote rows, and empty rows. The list will contain 52 lists, one for each of the fifty states plus the District of Columbia and one summative row (the row labeled "U.S."). The order of the list will be the same as the order in the file. If you wish, you may use the fact that Wyoming is the last data line of the file to terminate your loop. (Hint: use csv.reader), see notes below.) 2. def get_totals (L) : this function takes the list of lists retumed by the read_file () function and retuns two values. The data of interest for this function is the column at index 1 unauthorized immigrant population. Return the value in the summative row (labeled "U.s.") and the sum of the other 51 data rows. The purpos question #1 above. e of this function is to gather data to answer 3. def get_largest_states (L) this function takes the list of lists retuned by the read file () function and returns a list. The data of interest for this function is the column at index 2: unauthorized immigration % of population. The returned list is a list of states whose value is greater than the summative value (the value in the row labeled "U.S."). Since "District of Columbia" is in the file we will include it as a "state." The returned list will be in alphabetical order (the order of the original file). The purpose of this function is to gather data to answer question #2 above. def read file function and returns a list of lists. The data of interest for this function is the column at index 9: industry with largest number of unauthorized immigrant workers. The list of lists returned is a list of industries and occurences in the column (excluding the summative data from the row labeled "U.S."). The retumed list will be sorted by occurrences (largest first) and will look like this: [ [industryl,countl], [industry2,count2],..] The purpose of this function is to gather data to answer question #3 above. (Hint use key-itemgetter (1) to sort on index 1 of the lists.) 4. get_industry_counts (L) : this function takes the list of lists returned by the Notes and Suggestions 1. Using the CSV package: Remember import csv reader = csv. reader (fp) next (reader, None) for line in reader: # attaches a reader to the file fp # skips a line, such as a header line # line is a list Demonstrate your completed program to your TA. On-line students should submit the completed program (named "lab07.") for grading via the Mirmir system
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started