Offline Notepad View raw

Shared snapshot

hiiii

import matplotlib.pyplot as plt

Countries = {'US': {'capital': 'Washington D.C.', 'car_per_cap': 809, 'population': 46.77}, 'Australia': {'capital': 'Vienna', 'car_per_cap': 731, 'population': 66.03}, 'Japan': {'capital': 'Tokyo', 'car_per_cap': 588, 'population': 80.2}, 'Russia': {'capital': 'Moscow', 'car_per_cap': 18, 'population': 5.084}, 'India': {'capital': 'New Delhi', 'car_per_cap': 18, 'population': 5.084}, 'Morocco': {'capital': 'Rabat', 'car_per_cap': 18, 'population': 55.4}}

Print country and their capital names from the dictionary

print("Country and their capital names:") for country, info in Countries.items(): print(f"{country} : {info['capital']}")

Find top 3 countries having highest population and print the same

sorted_countries_by_population = sorted(Countries.items(), key=lambda x: x[1]['population'], reverse=True)[:3] print("Top 3 countries with highest population:") for country, info in sorted_countries_by_population: print(f"{country} : {info['population']}")

Sort a dictionary by car_per_cap and print the Country, Capital and Car_Per_Cap

sorted_countries_by_car_per_cap = sorted(Countries.items(), key=lambda x: x[1]['car_per_cap']) print("Country, Capital and Car_Per_Cap:") for country, info in sorted_countries_by_car_per_cap: print(f"{country} : {info['capital']}, {info['car_per_cap']}")

Plot bar graph to display country-wise population ratio

countries = [] populations = [] for country, info in Countries.items(): countries.append(country) populations.append(info['population']) plt.bar(countries, populations) plt.title("Country-wise population ratio") plt.xlabel("Country") plt.ylabel("Population (in millions)") plt.show()