from datascience import *
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')New material¶
Python fundamentals¶
Python as a calculator:
2 + 92 * 4# The line below errors
# * 42 ** 4# The line below errors
# 2 * * 42/9Python understands the order of operations!
2 + 3 * 9(2 + 3) * 9-1 - 1 + 2 * (3 * 4 * 5 / 6) ** 3 + 7 + 8 + 12Floating point vs integer representation
864 / 8Strings!
'hello''Data 8'"hello"'I don't ever want to miss class'"I don't ever want to miss class"Names¶
a = 2aNames must first be assgined to a value! The line below causes a name error since b isn’t assigned yet.
bb = 3bDiscussion Question 1: What will the value of a be once the code in the following cell has finished running?
a = 2
a + 5a = a + 5
aa * babDiscussion Question 2: What will the value of total be once the code in the following cell has finished running?
total = a + b
total
a = 10
totaltotalatotal = a + b
totalThe line below causes a syntax error.
a + b = totalWhy Names?¶
A cell with arithmetic but no names is difficult to interpret.
Example: On January 1, 2025, the CA Minimum Wage for Employers with 26 Employees or More increased to $16.50/hour. Calculate the yearly minimum wage of an employee who works for such an employer.
Method 1:
40 * 16.540 * 52 * 16.5Method 2:
ca_hourly_minimum_wage = 16.50
hours_per_week = 40
weeks_per_year = 52weekly_wages = hours_per_week * ca_hourly_minimum_wage
weekly_wagesyearly_wages = weekly_wages * weeks_per_year
yearly_wagesCall Expressions and Arguments¶
abs(-5)abs(1 - 3)Names can help us with inputs (called arguments in computer science jargon) to functions!
day_temp = 52
night_temp = 47
abs(night_temp - day_temp)Functions can take multiple arguments.
min(14, 15)Some functions can be called with a single argument or with multiple arguments!
round(123.456)round(123.456, 1)STOP
round(123.456, ndigits=1)In the third line, ndigits is a named argument.
How did I know that ndigits would work as a name? You can use ? to figure out everything you want to know about a function.
round?round(number=123.456)The line below causes a type error because the wrong name was used.
round(123.456, digs=1)Tables¶
Example: Ice cream¶
cones = Table.read_table('cones.csv')
conescones.show(3)cones.show()Table Operations
cones.select('Flavor')cones.select('Flavor', 'Price')The line below causes an error because Flavor is not a name, but rather, a column in the cones table.
cones.select(Flavor, 'Price')cones.drop('Price')conescones_without_price = cones.drop('Price')
cones_without_pricecones.where('Flavor', 'chocolate')cones.sort('Price')cones.sort('Price', descending=True)cones.sort('Flavor', descending=True)Example 2: Basketball¶
# From https://github.com/erikgregorywebb/datasets/blob/master/nba-salaries.csv
nba = Table.read_table('nba_salaries.csv')
nbapoint_guards = nba.where('position', 'PG').where('season', 2020)point_guardspoint_guards.drop('position')point_guardspoint_guards = point_guards.drop('rank', 'position', 'season')point_guards.show(10)point_guards.sort('salary').show(10)point_guards.sort('salary', descending=True).show(10)The order of the computations does matter!
Discussion Question: Which one of the following two lines of code fail?
nba.drop('position').where('position', 'PG')nba.where('position', 'PG').drop('position')