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 random
# Import the two datasets
gapminder = pd.read_csv("datasets/gapminder.csv")
brics = pd.read_csv("datasets/brics.csv")Take Notes
- The country with mas population is China!
# Add your code snippets here
print(brics)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 country, population in zip(brics['country'], brics['population']):
print(f"The population of {country} is {population} million!")print(gapminder)# Filter for African countries
africa_life_exp = gapminder[gapminder['cont'] == 'Africa']['life_exp']
# Create the histogram
plt.hist(africa_life_exp, bins=20) # Adjust the number of bins as needed
# Add title and axis labels
plt.title('Distribution of Life Expectancy in Africa')
plt.xlabel('Life Expectancy (years)')
plt.ylabel('Number of Countries')
# Show the plot
plt.show()# Simulate 10 rolls of two dice
for roll in range(10):
# Simulate rolling two dice
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
dice_sum = die1 + die2
# Show the result of the roll
print(f"Roll {roll + 1}: Die 1 = {die1}, Die 2 = {die2}, Sum = {dice_sum}")
# Check the conditions
if dice_sum == 7 or dice_sum == 11:
print("A win!")
elif dice_sum == 2 or dice_sum == 3 or dice_sum == 12:
print("A loss!")
else:
print("Roll again!")