Generalise the code written for exercise 1.1.3 for finding which European countries have the largest population in 1952 and 2007 by creating a function that finds which country on a defined continent has the largest population for a given year. Provide default values for certain arguments.
Note: import the necessary libraries the function needs: statistics, csv and os.
The function should start with:
def largest_country_by_continent_and_year(gapminder_filepath, continent='', year=''):
and end with:
print(continent, 'largest country in', year)
return largest_country, pop
Using the following lines of code, you can validate your script:
largest_country = largest_country_by_continent_and_year(os.path.join('path', 'to', 'file.csv'), 'Africa', '2007')
print(largest_country)
It should give the following line of code:
Africa largest country in 2007
('Nigeria', 135031164)