Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Aggregating and Summarizing Government Data | Data Analysis for Public Sector Insights
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Government Analysts

bookAggregating and Summarizing Government Data

1234567891011121314
# Summing up total population across multiple regions using a for loop regions = [ {"name": "North District", "population": 120000}, {"name": "East District", "population": 95000}, {"name": "South District", "population": 78000}, {"name": "West District", "population": 110000} ] total_population = 0 for region in regions: total_population += region["population"] print("Total population across all regions:", total_population)
copy
12345678910111213141516171819202122232425262728
# Finding the region with the highest median income regions = [ {"name": "North District", "incomes": [40000, 42000, 41000, 45000]}, {"name": "East District", "incomes": [39000, 39500, 38500, 40000]}, {"name": "South District", "incomes": [37000, 36000, 37500, 38000]}, {"name": "West District", "incomes": [47000, 48000, 46000, 49000]} ] def median(values): sorted_vals = sorted(values) n = len(sorted_vals) mid = n // 2 if n % 2 == 0: return (sorted_vals[mid - 1] + sorted_vals[mid]) / 2 else: return sorted_vals[mid] highest_median = None highest_region = None for region in regions: region_median = median(region["incomes"]) if highest_median is None or region_median > highest_median: highest_median = region_median highest_region = region["name"] print("Region with the highest median income:", highest_region)
copy
question mark

Select the correct answer

question mark

Select the correct answer

question mark

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain how the median function works in this example?

What other summary statistics can I calculate with this data?

How can I modify the code to find the region with the lowest median income?

bookAggregating and Summarizing Government Data

Stryg for at vise menuen

1234567891011121314
# Summing up total population across multiple regions using a for loop regions = [ {"name": "North District", "population": 120000}, {"name": "East District", "population": 95000}, {"name": "South District", "population": 78000}, {"name": "West District", "population": 110000} ] total_population = 0 for region in regions: total_population += region["population"] print("Total population across all regions:", total_population)
copy
12345678910111213141516171819202122232425262728
# Finding the region with the highest median income regions = [ {"name": "North District", "incomes": [40000, 42000, 41000, 45000]}, {"name": "East District", "incomes": [39000, 39500, 38500, 40000]}, {"name": "South District", "incomes": [37000, 36000, 37500, 38000]}, {"name": "West District", "incomes": [47000, 48000, 46000, 49000]} ] def median(values): sorted_vals = sorted(values) n = len(sorted_vals) mid = n // 2 if n % 2 == 0: return (sorted_vals[mid - 1] + sorted_vals[mid]) / 2 else: return sorted_vals[mid] highest_median = None highest_region = None for region in regions: region_median = median(region["incomes"]) if highest_median is None or region_median > highest_median: highest_median = region_median highest_region = region["name"] print("Region with the highest median income:", highest_region)
copy
question mark

Select the correct answer

question mark

Select the correct answer

question mark

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
some-alt