Skip to content
4 hidden cells
Intermediate Python exercises
Intermediate Python
Run the hidden code cell below to import the data used in this course.
# Import the course packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Import the two datasets
gapminder = pd.read_csv("datasets/gapminder.csv")
brics = pd.read_csv("datasets/brics.csv")4 hidden cells
Explore Datasets
Use the DataFrames imported in the first cell to explore the data and practice your skills!
- Create a loop that iterates through the
bricsDataFrame and prints "The population of {country} is {population} million!". - Create a histogram of the life expectancies for countries in Africa in the
gapminderDataFrame. Make sure your plot has a title, axis labels, and has an appropriate number of bins. - Simulate 10 rolls of two six-sided dice. If the two dice add up to 7 or 11, print "A win!". If the two dice add up to 2, 3, or 12, print "A loss!". If the two dice add up to any other number, print "Roll again!".
for lab, row in brics.iterrows():
print("The population of " + row['country'] + " is " + str(row['population']))import matplotlib.pyplot as plt
import pandas as pd
life_expectancies = gapminder[gapminder['cont'] == 'Africa']['life_exp']
plt.hist(life_expectancies, bins=20, edgecolor='black')
plt.title('Life Expectancies in African Countries')
plt.xlabel('Life Expectancy')
plt.ylabel('Frequency')
plt.show()import random
for x in range(10):
roll = random.randint(1, 6) + random.randint(1, 6)
if roll == 7 or roll == 11:
print("A win!")
elif roll == 2 or roll == 3 or roll == 12:
print("A loss!")
else:
print("Roll again!")