Skip to content

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 brics DataFrame and prints "The population of {country} is {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.
  • 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

  1. I created a for loop to iterate over the rows of the brics DataFrame.
  2. I used .iterrows() to go through each row one by one.
  3. Inside the loop, I accessed the country and population columns 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:

  1. I filter for African countries using cont == 'Africa' and storage them in africa_data
  2. I created the histogram of life expectancy by the name of life_exp with 10 bins
  3. I added the title and the labels for the axes.
  4. 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:

  1. I set a random seed using np.random.seed() to ensure reproducibility.
  2. I generated two randon numbers between 1 and 6 using np.random.randint(1, 7) simulating a six-sided dice. These were store as dice1 and dice2
  3. I used an if statement to check the sum of the two dice and print the expected results
  4. I used a for loop 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!")