Intermediate Python
👋 Welcome to your workspace! Here, you can write and run Python code and add text. The purpose of this workspace is to allow you to experiment with the data from Intermediate Python and practice your newly learned skills with some challenges. You can find out more about DataCamp Workspace here.
Cells with text (such as this one) are Markdown cells. Markdown cells can contain notes, explain code, and summarize findings. As this may be your first workspace, we have included some steps to get you started!
1. Get Started
Below is a code cell. It is used to execute Python code. The code below imports three packages you used in Intermediate Python: numpy, pandas, and matplotlib. The code also imports data you used in the course using the pandas read_csv() funtion.
🏃To execute the code, select the cell and click "Run" or the ► icon. You can also use Shift-Enter to run a selected cell.
# Importing course packages; you can add more too!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Import the three datasets
gapminder = pd.read_csv("datasets/gapminder.csv")
cars = pd.read_csv("datasets/cars.csv")
brics = pd.read_csv("datasets/brics.csv")
# Print the first DataFrame
gapminder# Importing course packages; you can add more too!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Import the three datasets
gapminder = pd.read_csv("datasets/gapminder.csv")
cars = pd.read_csv("datasets/cars.csv")
brics = pd.read_csv("datasets/brics.csv")
# Print the first DataFrame
print(gapminder)2. Write Code
After running the cell above, you have created three pandas DataFrames: gapminder, cars, and brics.
Try one (or more) of the following tasks to get you started. Don't forget to add more code cells if you need them. This is your place to experiment!
- Create a loop that iterates through the
bricsDataFrame and prints "The population of {country} is {population} million!". If you're stuck, try reviewing this video. - 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. If you're stuck, try reviewing this video and this video. - 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!". If you're stuck, try reviewing this video.
Be sure to check out the Answer Key at the end to see one way to solve each problem. Did you try something similar?
# Replace the code below and try one or more of the three challenges above!
for lab, row in brics.iterrows():
print("the population of " + str(row["country"]) + " Is " + str(row["population"]) + " million")gapminder_africa = gapminder[gapminder.cont == "Africa"]
plt.hist(gapminder_africa["life_exp"], bins=20)
plt.title("Distribution of Life Expectancies in African Countries")
plt.xlabel("Life Expectancy")
plt.ylabel("Count")
plt.show()for x in range(10):
dice_one = np.random.randint(1, 7)
dice_two = np.random.randint(1, 7)
if dice_one + dice_two == 7 or dice_one + dice_two == 11:
print("A win!")
elif (
dice_one + dice_two == 2
or dice_one + dice_two == 3
or dice_one + dice_two == 12
):
print("A loss!")
else:
print("Roll again!")Feeling confident about your skills? Continue on to Data Manipulation with pandas! This course will help you continue to develop essential data science skills with pandas, and set you up for more advanced techniques such as visualization and machine learning!
Answer Key
Below are potential solutions to the challenges shown above. Try them out and see how they compare to how you approached the problem!
# 1. Create a loop that prints "The population of {country} is {population}!"
for lab, row in brics.iterrows():
print(
"The population of "
+ str(row["country"])
+ " is "
+ str(row["population"])
+ " million!"
)# 2. Create a histogram of the life expectancies for countries in Africa in the `gapminder` DataFrame.
gapminder_africa = gapminder[gapminder.cont == "Africa"]
plt.hist(gapminder_africa["life_exp"], bins=20)
plt.title("Distribution of Life Expectancies in African Countries")
plt.xlabel("Life Expectancy")
plt.ylabel("Count")
plt.show()# 3. Create a simulation of 10 rolls of two six-sided dice.
for x in range(10):
dice_one = np.random.randint(1, 7)
dice_two = np.random.randint(1, 7)
if dice_one + dice_two == 7 or dice_one + dice_two == 11:
print("A win!")
elif (
dice_one + dice_two == 2
or dice_one + dice_two == 3
or dice_one + dice_two == 12
):
print("A loss!")
else:
print("Roll again!")