Skip to content
1 hidden cell
Intermediate Python
Intermediate Python
Run the hidden code cell below to import the data used in this course.
1 hidden cell
Take Notes
Add notes about the concepts you've learned and code cells with code you want to keep.
Add your notes here
# Line Plot 1
import matplotlib.pyplot as plt
plt.plot(x,y)
plt.show()
# Show last item in list
print(x[-1])
# Scatter Plot 1
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.show()
# Put the x-axis on a logarithmic scale
# Basic scatter plot, log scale
plt.scatter(x, y)
plt.xscale('log')
# Histograms
plt.hist(x)
plt.show()
# Show and clear plot again
plt.show()
plt.clf()
___________________________________________________________
SIZES
# Import numpy as np
import numpy as np
# Store pop as a numpy array: np_pop
np_pop = np.array(pop)
# Double np_pop
np_pop = np_pop * 2
# Update: set s argument to np_pop
plt.scatter(gdp_cap, life_exp, s = np_pop)
# Previous customizations
plt.xscale('log')
plt.xlabel('GDP per Capita [in USD]')
plt.ylabel('Life Expectancy [in years]')
plt.title('World Development in 2007')
plt.xticks([1000, 10000, 100000],['1k', '10k', '100k'])
# Display the plot
plt.show()
COLORS
?
GRID LINES
# Show Grid Lines using call grid()
plt.grid(True)
___________________________________________________________
DICTIONARIES (1)
LIST
pop = [1, 2, 3]
countries = ["abv", "abl", "yau"]
in_alb = countries.index("albania")
ind_alb
pop[ind_alb]
Not Convenient, Not intuitive
# Create Dictionary
{Key Value Pairs}
{"afghanistan":30.55,}
world = {}
world["albania"]
Search superfast in dictionaries
Line Plots: When you have a time scale along the horizontal axis, the line plot is your friend. But in many other cases, when you're trying to assess if there's a correlation between two variables, for example, the scatter plot is the better choice.
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!".