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")

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Add your notes here

# Add your code snippets here
for index, row in brics.iterrows():
    country = row["country"]
    population = row["population"]
    print(f"The popiulation of {country} is {population} millions!")
#Histogram of Life expectancy for Africa 
Africa = gapminder[gapminder["cont"] == "Africa"] #Subsetting Africa rows 
plt.hist(Africa["life_exp"], bins=20, color="skyblue", edgecolor="black")#Plotting histogram 
plt.xlabel("life Expectancy (years)")
plt.ylabel("Number of countrie")
plt.title("Life Expectancy in Africa")
plt.show()
#Dice Simulation 
import random

for i in range(10):
    die1 = random.randint(1, 6) #roll first die 
    die2 = random.randint(1, 6) #roll second die
    total = die1 + die2

    print(f" Roll {i + 1}: {die1} + {die2} = {total}")

    if total in [7, 11]:
        print("A win!")
    elif total in [2, 3, 12]:
        print("A loss!")
    else:
        print("Roll again!")

Find in the hidden cell below some exercises to explore the data and practice your skills:

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!".