Skip to content

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.

help(plt.hist) x should be a list of values you want to build a histogram for, bins tells python how many bins. If you dont specify then it will be 10 by default

print () - display x[-1] retrieve data in array, it starts from 0, -1 to start from end import matplotlib.pyplot as plt

plt.show()- display graph plt.plot(x,y) plt.scatter(x,y)

plt.hist(values, bins)/ plt.hist(values)

//Add labels to axis on graph plt.xlabel('Year') plt.ylabel('Popullation') plt.Title('World Population Projection') plt.yticks([0,2,4,6,8,10], [0B, 2B, 4B, 6B, 10B]) // changes the y axis to vales in billions

year = [1800,1850,1900] + year // Adds additional data to an existing list

plt.xscale('log') logarithmetic scale

col - color

alpha is opacity ranges between 0 & 1

plt.text() plt.grid(TRUE) \ Adds gridlines

Index ind_alb = countries.index("alabama") // Fetch index number pop[ind_alb]

Dictionaries

world = {"Afghan": 20.88, "Iran":20} world [albania]// Fetches data aligned in dictionary

//Pandas is an open source library, providing high-performance, easy-to-use data structures and data analysis tools for Python.

//The DataFrame is one of Pandas' most important data structures. It's basically a way to store tabular data where you can label the rows and the columns. One way to build a DataFrame is from a dictionary.

cars = pd.DataFrame(cars_dict)

Import the cars.csv data: cars

cars = pd.DataFrame(pd.read_csv('cars.csv'))

Fix import by including index_col

cars = pd.read_csv('cars.csv',index_col = 0)

brics= pd.read_csv("path/to/brics.csv",index_col = 0) // importing CSV file into dataframe as brics

//Select specific columns in a dataframe , use double brackets and columns needed bric[["country", "capital"]]

//select specific rows in a dataframe, use indexing/slicing bric[1:4]

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