''' Honors Series Data Science Fall 2020 Mini-Assignment #2 - Solutions ''' AGE = 5 RACE = 7 def main(): # Part 1 - Average, min, max of a list of numbers lst = [2.5, 4.87, 6.0, 8.2] avg = sum(lst) / len(lst) mx = max(lst) mn = min(lst) print('Avg:', avg) print('Max:', mx) print('Min:', mn) print('Here was your list:', lst) # Part 2 - Print 2x each number lst2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] for num in lst2: num = int(num) print(num * 2) # Part 2 - Percent change in foreclosures fores = [11, 10, 6, 8, 4, 13, 11, 8, 6, 10, 3, 4, 10, 10, 6, 2, 6, 8, 10, 11, 14, 6, 10, 6] pct_change = (fores[5] - fores[0]) / fores[0] print('Percent change from January to June:', pct_change) # Part 3 - Get the maximum of a column in a nested list nested = [['a', 2015, 27, 'M'], ['b', 2015, 33, 'M'], ['c', 2015, 23, 'M'], ['d', 2015, 32, 'M'], ['e', 2015, 39, 'M']] ages = [] for row in nested: ages.append(row[2]) print('Oldest person in the list was age', max(ages)) # Part 4 - Work with the actual dataset data = [['3', 'Tim Elliot', '2015-01-02', 'shot', 'gun', '53', 'M', 'A', 'Shelton', 'WA', 'True', 'attack', 'Not fleeing', 'False'], ['4', 'Lewis Lee Lembke', '2015-01-02', 'shot', 'gun', '47', 'M', 'W', 'Aloha', 'OR', 'False', 'attack', 'Not fleeing', 'False'], ['5', 'John Paul Quintero', '2015-01-03', 'shot and Tasered', 'unarmed', '23', 'M', 'H', 'Wichita', 'KS', 'False', 'other', 'Not fleeing', 'False'], ['8', 'Matthew Hoffman', '2015-01-04', 'shot', 'toy weapon', '32', 'M', 'W', 'San Francisco', 'CA', 'True', 'attack', 'Not fleeing', 'False'], ['9', 'Michael Rodriguez', '2015-01-04', 'shot', 'nail gun', '39', 'M', 'H', 'Evans', 'CO', 'False', 'attack', 'Not fleeing', 'False'], ['11', 'Kenneth Joe Brown', '2015-01-04', 'shot', 'gun', '18', 'M', 'W', 'Guthrie', 'OK', 'False', 'attack', 'Not fleeing', 'False'], ['13', 'Kenneth Arnold Buck', '2015-01-05', 'shot', 'gun', '22', 'M', 'H', 'Chandler', 'AZ', 'False', 'attack', 'Car', 'False'], ['15', 'Brock Nichols', '2015-01-06', 'shot', 'gun', '35', 'M', 'W', 'Assaria', 'KS', 'False', 'attack', 'Not fleeing', 'False'], ['16', 'Autumn Steele', '2015-01-06', 'shot', 'unarmed', '34', 'F', 'W', 'Burlington', 'IA', 'False', 'other', 'Not fleeing', 'True']] ages = [] for row in data: ages.append(int(row[AGE])) print('The oldest person in this part of the data is', max(ages), 'years old') print('The youngest person in this part of the data is', min(ages), 'years old') average = sum(ages) / len(ages) print('The average age of people in our dataset is', round(average, 2)) hispanic = 0 for row in data: if row[RACE] == 'H': hispanic = hispanic + 1 print('There are', hispanic, 'Hispanic individuals listed in the dataset') main()