Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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 + 9
2 * 4
# The line below errors
# * 4
2 ** 4
# The line below errors 
# 2 * * 4
2/9

Python understands the order of operations!

2 + 3 * 9
(2 + 3) * 9
-1 - 1 + 2 * (3 * 4 * 5 / 6) ** 3 + 7 + 8 + 12

Floating point vs integer representation

8
64 / 8

Strings!

'hello'
'Data 8'
"hello"
'I don't ever want to miss class'
"I don't ever want to miss class"

Names

a = 2
a

Names must first be assgined to a value! The line below causes a name error since b isn’t assigned yet.

b
b = 3
b

Discussion Question 1: What will the value of a be once the code in the following cell has finished running?

a = 2 
a + 5
a = a + 5
a

a * b
a
b

Discussion 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
total

total
a
total = a + b
total

The line below causes a syntax error.

a + b = total

Why 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.5
40 * 52 * 16.5

Method 2:

ca_hourly_minimum_wage = 16.50
hours_per_week = 40
weeks_per_year = 52
weekly_wages = hours_per_week * ca_hourly_minimum_wage
weekly_wages
yearly_wages = weekly_wages * weeks_per_year
yearly_wages

Call 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')
cones
cones.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')
cones
cones_without_price = cones.drop('Price')
cones_without_price
cones.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')
nba
point_guards = nba.where('position', 'PG').where('season', 2020)
point_guards
point_guards.drop('position')
point_guards
point_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')