Skip to content

Intermediate Python

Run the hidden code cell below to import the data used in this course.

x = 8
y = 9
#not(not(x < 3) and not(y > 14 or y > 10))
False and True

Take Notes

Distionaries, Part 1 pop = [30.55, 2.77, ....] countries = ["afganistan", "albania", ....] ind_alb = countries.index("albania") print(ind_alb) #result will be 1 print(pop[ind_alb]) #result will be 2.77

world = {"afganistan":30.55, "albania":2.77, ....}

Definition of dictionary

europe = { 'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' } print(europe.keys()) #Prints out the keys in europe dict_keys(['spain', 'france', 'germany', 'norway']) #this is a result

europe['norway'] # use square brackets for keys 'oslo

europe['italy'] = 'rome' #Add italy to europe with capital of "rome" print('italy' in europe) #check if Italy is in 'Europe' dict. Answer should be True / False del(europe["australia"]) #Removes australia

print(europe['france']["population"]) # it will print population of France!! data = {"capital":"rome" , "population":"59.83"} # Create sub-dictionary data europe["italy"] = data # Add data to europe under key 'italy'

Pandas

names = ['United States', 'Australia', 'Japan', 'India', 'Russia', 'Morocco', 'Egypt'] dr = [True, False, False, False, True, True, True] cpc = [809, 731, 588, 18, 200, 70, 45] import pandas as pd my_dict = {'country':(names), #don't use square brackets as it will create a list. 'drives_right':(dr), 'cars_per_cap':(cpc)} cars = pd.DataFrame(my_dict) print(cars)

row_labels = ['US', 'AUS', 'JPN', 'IN', 'RU', 'MOR', 'EG']

cars.index = row_labels #Specify row labels of cars

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

**loc and iloc

print(cars["country"]) print(cars[["country"]]) print(cars[["country","drives_right"]]) #result will contain 2 columns print(cars[0:3]) #prints first 3 rows print(cars[3:6]) #prints out fourth, fifth and sixth observation

print(cars.loc["JPN"]) # prints observations print(cars.iloc[[1,6]]) # double brackets for complete table format

print(cars.loc["MOR" , "drives_right"]) print(cars.loc[["RU" , "MOR"],['country' , 'drives_right']])

True #answers country drives_right RU Russia True MOR Morocco True

print(cars.loc[: , ["drives_right"]]) #prints as data frame print(cars.loc[: , ["cars_per_cap" ,"drives_right"]]) #prints as data frame

Comparision Operators < , > , >= , <= , == , != np.logical_and(), np.logical_or() and np.logical_not() #it is for NumPy Array import numpy as np my_house = np.array([18.0, 20.0, 10.75, 9.50]) np.logical_or(my_house > 18.5 , my_house < 10) np.logical_and(my_house < 11 , your_house < 11)

if, else, elif room = "bed" area = 14.0 if area > 15 : #use colon print("big place!")

if room == "kit" : print("looking around in the kitchen.") elif room == "bed": # <<< SEMICOLON has to be inserted after each statement!!!! print("looking around in the bedroom.") else : print("looking around elsewhere.")

Filtering pandas DataFrames

table["column1"] #select the Pandas series column (possible with loc or iloc) new_table = table["column1"] >10 Subset DF = table["new_table"] or table[table["column1"]>10]

Boolean operators import numpy as np Step1: logical_and(table["column1"] > 10, table["column1"] <20) Step2: table[np.logical_and(table["column1"] > 10, table[column1] <20]

import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) cpc = cars["cars_per_cap"] many_cars = cars["cars_per_cap"] > 500 car_maniac = cars[many_cars] #we are subsetting 'cars'table

np.logical_and(), np.logical_or() and np.logical_not()

cpc = cars['cars_per_cap'] between = np.logical_and(cpc > 100, cpc < 500) medium = cars[between]

**while loop

x = 1 while x < 4 : print(x) x = x + 1

Add your notes here

# Add your code snippets here

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