Covering the materials of Chapters 7-8.
Topics: collection data structures, object oriented programming
In the following 4 lists you will find the country name, capital city name, area (in km2) and population (in millions) data for 43 European countries respectively.
countries = ['Albania', 'Andorra', 'Austria', 'Belgium', 'Bosnia and Herzegovina', 'Bulgaria', 'Czech Republic', 'Denmark', 'United Kingdom', 'Estonia', 'Belarus', 'Finland', 'France', 'Greece', 'Netherlands', 'Croatia', 'Ireland', 'Iceland', 'Kosovo', 'Poland', 'Latvia', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macedonia', 'Hungary', 'Malta', 'Moldova', 'Monaco', 'Montenegro', 'Germany', 'Norway', 'Italy', 'Portugal', 'Romania', 'San Marino', 'Spain', 'Switzerland', 'Sweden', 'Serbia', 'Slovakia', 'Slovenia', 'Ukraine']
capitals = ['Tirana', 'Andorra la Vella', 'Vienna', 'Brussels', 'Sarajevo', 'Sofia', 'Prague', 'Copenhagen', 'London', 'Tallin', 'Minsk', 'Helsinki', 'Paris', 'Athens', 'Hague', 'Zagreb', 'Dublin', 'Reykjavik', 'Prishtina', 'Warsaw', 'Riga', 'Vaduz', 'Vilnius', 'luxembourg', 'Skopje', 'Budapest', 'Valletta', 'Chisinau', 'Monaco', 'Podgorica', 'Berlin', 'Oslo', 'Rome', 'Lisbon', 'Bucharest', 'San Marino', 'Madrid', 'Berne', 'Stockholm', 'Belgrade', 'Bratislava', 'Ljubljana', 'Kiev']
areas = [28748, 468, 83857, 30519, 51130, 110912, 78864, 43077, 244100, 45100, 207600, 338145, 543965, 131957, 33933, 56500, 70283, 103000, 10887, 312683, 63700, 160, 65200, 2586, 25713, 93036, 316, 33700, 2, 13812, 357042, 323877, 301277, 92389, 237500, 61, 504782, 41293, 449964, 66577, 49035, 20250, 603700]
populations = [3.2, 0.07, 7.6, 10.0, 4.5, 9.0, 10.4, 5.1, 57.2, 1.6, 10.3, 4.9, 56.2, 10.0, 14.8, 4.7, 3.5, 0.3, 2.2, 37.8, 2.6, 0.03, 3.6, 0.4, 2.1, 10.4, 0.3, 4.4, 0.03, 0.6, 78.6, 4.2, 57.5, 10.5, 23.2, 0.03, 38.8, 6.7, 8.5, 7.2, 5.3, 2.0, 51.8]
Let's display the data stored in all lists:
print("Countries:")
print(countries)
print("----------")
print("Capitals:")
print(capitals)
print("----------")
print("Areas (in km2):")
print(areas)
print("----------")
print("Populations (in millions):")
print(populations)
The index position of the elements in the lists ties the information for each country together:
for idx in range(len(countries)):
print("Name: %s, Capital: %s, Area: %d km2, Population: %.2f millions" % (countries[idx], capitals[idx], areas[idx], populations[idx]))
Storing the data in 4 separate lists is not comfortable. Construct a list of dictionaries programatically:
The result should be like the following:
[
{
'country': 'Albania',
'capital': 'Tirana',
'area': 28748,
'population': 3.2
},
...
{
'country': 'Ukraine',
'capital': 'Kiev',
'area': 603700,
'population': 51.8
}
]
dataset = []
for idx in range(len(countries)):
dataset.append({
'country': countries[idx],
'capital': capitals[idx],
'area': areas[idx],
'population': populations[idx]
})
print(dataset)
Calculate the population density for each country (in people / km2 unit) and extends each country with this information.
The result should be like the following:
[
{
'country': 'Albania',
'capital': 'Tirana',
'area': 28748,
'population': 3.2,
'density': 111.31209127591485
},
...
{
'country': 'Ukraine',
'capital': 'Kiev',
'area': 603700,
'population': 51.8,
'density': 85.80420738777539
}
]
for item in dataset:
item['density'] = item['population'] * 1e6 / item['area']
print(dataset)
Find the country with the highest population density.
max_idx = 0
for idx in range(1, len(dataset)):
if dataset[idx]['density'] > dataset[max_idx]['density']:
max_idx = idx
print(dataset[max_idx])
Task A): define a class named Country
, which can store a country's name, capitaly city, area and population.
Construct a list of objects, where each object is an instance of the Country
class.
Task B): add a density()
method to the Country
class, which calculates the population density for that country dynamically.
Find the country with the highest population density.
class Country():
def __init__(self, name, capital, area, population):
self.name = name
self.capital = capital
self.area = area
self.population = population
def density(self):
return self.population * 1e6 / self.area
dataset2 = []
for idx in range(len(countries)):
dataset2.append(Country(countries[idx], capitals[idx], areas[idx], populations[idx]))
max_idx = 0
for idx in range(1, len(dataset2)):
if dataset2[idx].density() > dataset2[max_idx].density():
max_idx = idx
print(dataset2[max_idx].name)