Skip to content
Intermediate Python
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")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!".
 
Take Notes
Add notes about the concepts you've learned and code cells with code you want to keep.
This is the table I'm going to work on.
print(gapminder)- Create a loop that iterates through the brics DataFrame and prints "The population of {country} is {population} million!".
 
Add your notes here
- I created a 
forloop to iterate over the rows of thebricsDataFrame. - I used 
.iterrows()to go through each row one by one. - Inside the loop, I accessed the 
countryandpopulationcolumns to print the required statement. 
for i, row in brics.iterrows():
    print(f"The population of {row['country']} is {row['population']} million!")- Create a histogram of the life expectancies for countries in Africa in the gapminder DataFrame. Make sure your plot has a title, axis labels, and has an appropriate number of bins.
 
Notes:
- I filter for African countries using 
cont == 'Africa'and storage them inafrica_data - I created the histogram of life expectancy by the name of 
life_expwith 10 bins - I added the title and the labels for the axes.
 - I displayed the plot.
 
africa_data = gapminder[gapminder['cont'] == 'Africa']
plt.hist(africa_data['life_exp'], bins = 10)
plt.title("Life expentancy in Africa")
plt.xlabel("Life expentacy in years")
plt.ylabel("Num of countries")
plt.show()- 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!".
 
Notes:
- I set a random seed using 
np.random.seed()to ensure reproducibility. - I generated two randon numbers between 1 and 6 using 
np.random.randint(1, 7)simulating a six-sided dice. These were store asdice1anddice2 - I used an 
ifstatement to check the sum of the two dice and print the expected results - I used a 
forloop to simulate 10 throws of two dices and printed the outcomes. 
np.random.seed()
for x in range(10):
    dice1 = np.random.randint(1, 7)
    dice2 = np.random.randint(1, 7)
    
    if dice1 + dice2 == 7 or dice1 + dice2 == 11:
        print(str(dice1 + dice2) + " - A win!")
    elif dice1 + dice2 == 2 or dice1 + dice2 == 3 or dice1 + dice2 == 12:
        print(str(dice1 + dice2) + " - A loss!")
    else:
        print(str(dice1 + dice2)+ " - Roll again!")