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

Take Notes

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

Add your notes here

Loop through brics DataFrame

for index, row in brics.iterrows(): country = row['country'] population = row['population'] print(f"The population of {country} is {population} million!")

Create histogram of life expectancies for countries in Africa

africa_data = gapminder[gapminder['continent'] == 'Africa'] plt.hist(africa_data['lifeExp'], bins=10) plt.title('Life Expectancies in Africa') plt.xlabel('Life Expectancy') plt.ylabel('Frequency') plt.show()

Simulate 10 rolls of two six-sided dice

import random

for _ in range(10): dice1 = random.randint(1, 6) dice2 = random.randint(1, 6) total = dice1 + dice2

if total == 7 or total == 11: print("A win!") elif total == 2 or total == 3 or total == 12: print("A loss!") else: print("Roll again!")
import random
for _ in range(10):
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    total = dice1 + dice2
    
    if total == 7 or total == 11:
        print("A win!")
    elif total == 2 or total == 3 or total == 12:
        print("A loss!")
    else:
        print("Roll again!")
# Loop through brics DataFrame
for index, row in brics.iterrows():
    country = row['country']
    population = row['population']
    print(f"The population of {country} is {population} million!")

# Create histogram of life expectancies for countries in Africa
africa_data = gapminder[gapminder['cont'] == 'Africa']
plt.hist(africa_data['life_exp'], bins=10)
plt.title('Life Expectancies in Africa')
plt.xlabel('Life Expectancy')
plt.ylabel('Frequency')
plt.show()