def calculate_summary_stats(data_dict):
years = [row['Year'] for row in data_dict]
life_expectancies = [row['Life Expectancy'] for row in data_dict]
continents = [row['Continent'] for row in data_dict]
avg_life_expectancy = sum(life_expectancies) / len(life_expectancies)
min_life_expectancy = min(life_expectancies)
max_life_expectancy = max(life_expectancies)
min_year = years[life_expectancies.index(min_life_expectancy)]
max_year = years[life_expectancies.index(max_life_expectancy)]
return avg_life_expectancy, min_life_expectancy, max_life_expectancy, min_year, max_year
Find largest drop
def find_largest_drop(data_dict):
largest_drop = 0
largest_drop_year = 0
largest_drop_continent = ''
for i in range(len(data_dict) - 1):
drop = data_dict[i]['Life Expectancy'] - data_dict[i + 1]['Life Expectancy']
if drop > largest_drop:
largest_drop = drop
largest_drop_year = data_dict[i]['Year']
largest_drop_continent = data_dict[i]['Continent']
return largest_drop, largest_drop_year, largest_drop_continent
Plot life expectancy over time (text-based)
def plot_life_expectancy_over_time(years, life_expectancies):
print("\nLife Expectancy Over Time:")
for year, life_expectancy in zip(years, life_expectancies):
print(f"{year}: {'' int(life_expectancy)} ({life_expectancy:.2f})")
Plot life expectancy by continent (text-based)
def plot_life_expectancy_by_continent(continents, life_expectancies):
continent_life_expectancies = {}
for continent, life_expectancy in zip(continents, life_expectancies):
if continent not in continent_life_expectancies:
continent_life_expectancies[continent] = []
continent_life_expectancies[continent].append(life_expectancy)
print("\nLife Expectancy by Continent:")
for continent, life_expectancies in continent_life_expectancies.items():
avg_life_expectancy = sum(life_expectancies) / len(life_expectancies)
print(f"{continent}: {avg_life_expectancy:.2f}")
Calculate correlation between life expectancy and year
def calculate_correlation(years, life_expectancies):
sum_years = sum(years)
sum_life_expectancies = sum(life_expectancies)
sum_years_squared = sum(year2 for year in years)
sum_life_expectancies_squared = sum(life_expectancy2 for life_expectancy in life_expectancies)
sum_year_life_expectancy = sum(year life_expectancy for year, life_expectancy in zip(years, life_expectancies))
n = len(years)
correlation = (n sum_year_life_expectancy - sum_years sum_life_expectancies) / (
(n sum_years_squared - sum_years2) (n sum_life_expectancies_squared - sum_life_expectancies2))**0.5
return correlation
Main function
def main():
url = '(link unavailable)'
data_dict = load_data(url)
avg_life_expectancy, min_life_expectancy, max_life_expectancy, min_year, max_year = calculate_summary_stats(data_dict)
print("Summary Statistics:")
print(f"Average Life Expectancy: {avg_life_expectancy:.2f}")
print(f"Minimum Life Expectancy: {min_life_expectancy:.2f} in {min_year}")
print(f"Maximum Life Expectancy: {max_life_expectancy:.2f} in {max_year}")
largest_drop, largest_drop_year, largest_drop_continent = find_largest_drop(data_dict)
print(f"\nLargest Drop in Life Expectancy: {largest_drop:.2f} in {largest_drop_continent} from {largest_drop_year} to {largest_drop_year + 1}")
country = input("\nEnter a continent: ")
country_data = [row for row in data_dict if row['Continent'] == country]
if country_data:
country_life_expectancies = [row['Life Expectancy'] for row in country_data] country_min_life_expectancy = np.min(country_life_expectancies)
country_max_life_expectancy = np.max(country_life_expectancies)
country_avg_life_expectancy = np.mean(country_life_expectancies)
print(f"\nCorrelation between Life Expectancy and Year: {correlation:.2f}")
Load data
def load_data(url): import urllib.request response = urllib.request.urlopen(url) lines = response.read().decode().splitlines() data_dict = [] for line in lines[1:]: # Skip header values = line.split(',') data_dict.append({ 'Continent': values[0], 'Year': float(values[1]), 'Life Expectancy': float(values[2]) }) return data_dict
Calculate summary statistics
def calculate_summary_stats(data_dict): years = [row['Year'] for row in data_dict] life_expectancies = [row['Life Expectancy'] for row in data_dict] continents = [row['Continent'] for row in data_dict] avg_life_expectancy = sum(life_expectancies) / len(life_expectancies) min_life_expectancy = min(life_expectancies) max_life_expectancy = max(life_expectancies) min_year = years[life_expectancies.index(min_life_expectancy)] max_year = years[life_expectancies.index(max_life_expectancy)] return avg_life_expectancy, min_life_expectancy, max_life_expectancy, min_year, max_year
Find largest drop
def find_largest_drop(data_dict): largest_drop = 0 largest_drop_year = 0 largest_drop_continent = '' for i in range(len(data_dict) - 1): drop = data_dict[i]['Life Expectancy'] - data_dict[i + 1]['Life Expectancy'] if drop > largest_drop: largest_drop = drop largest_drop_year = data_dict[i]['Year'] largest_drop_continent = data_dict[i]['Continent'] return largest_drop, largest_drop_year, largest_drop_continent
Plot life expectancy over time (text-based)
def plot_life_expectancy_over_time(years, life_expectancies): print("\nLife Expectancy Over Time:") for year, life_expectancy in zip(years, life_expectancies): print(f"{year}: {'' int(life_expectancy)} ({life_expectancy:.2f})")
Plot life expectancy by continent (text-based)
def plot_life_expectancy_by_continent(continents, life_expectancies): continent_life_expectancies = {} for continent, life_expectancy in zip(continents, life_expectancies): if continent not in continent_life_expectancies: continent_life_expectancies[continent] = [] continent_life_expectancies[continent].append(life_expectancy) print("\nLife Expectancy by Continent:") for continent, life_expectancies in continent_life_expectancies.items(): avg_life_expectancy = sum(life_expectancies) / len(life_expectancies) print(f"{continent}: {avg_life_expectancy:.2f}")
Calculate correlation between life expectancy and year
def calculate_correlation(years, life_expectancies): sum_years = sum(years) sum_life_expectancies = sum(life_expectancies) sum_years_squared = sum(year2 for year in years) sum_life_expectancies_squared = sum(life_expectancy2 for life_expectancy in life_expectancies) sum_year_life_expectancy = sum(year life_expectancy for year, life_expectancy in zip(years, life_expectancies)) n = len(years) correlation = (n sum_year_life_expectancy - sum_years sum_life_expectancies) / ( (n sum_years_squared - sum_years2) (n sum_life_expectancies_squared - sum_life_expectancies2))**0.5 return correlation
Main function
def main(): url = '(link unavailable)' data_dict = load_data(url) avg_life_expectancy, min_life_expectancy, max_life_expectancy, min_year, max_year = calculate_summary_stats(data_dict) print("Summary Statistics:") print(f"Average Life Expectancy: {avg_life_expectancy:.2f}") print(f"Minimum Life Expectancy: {min_life_expectancy:.2f} in {min_year}") print(f"Maximum Life Expectancy: {max_life_expectancy:.2f} in {max_year}") largest_drop, largest_drop_year, largest_drop_continent = find_largest_drop(data_dict) print(f"\nLargest Drop in Life Expectancy: {largest_drop:.2f} in {largest_drop_continent} from {largest_drop_year} to {largest_drop_year + 1}") country = input("\nEnter a continent: ") country_data = [row for row in data_dict if row['Continent'] == country] if country_data: country_life_expectancies = [row['Life Expectancy'] for row in country_data] country_min_life_expectancy = np.min(country_life_expectancies)