14
Python
AI 101
Why Python?
On Python
- Python is free,
- Python is very widely used,
- Python is flexible,
- Python is relatively easy to learn,
- and Python is quite powerful.
Why not Python?
- Python is a general purpose language used for machine learning and artifical intelligence.
- Not for building apps, building software, managing databases, or developing user interfaces.
- For organizing thoughts and answering questions.
Running Example
Diving In
- Taking Python as a given, we’ll:
- Use an example of something I helped a student with recently
- Show step-by-step how to use Python
- Introduce a number of Python features to solve the problem more easily.
- This was from an introductory physics class I believe; I don’t know the context.
Motivating Example
- Recently, I helped a student stuck on this:
\[ f(x) = \begin{cases} 9x^2 + 5 & x < 4 \\ 9 & 4 \leq x \leq 8 \\ 2 - x & x > 8 \end{cases} \]
- Find \(f(x)\) for each of the following \(x\) values: \[ \{-1, 4, 5, 8, 11\} \]
Is this “real”?
- Models income tax brackets, one of the most important drivers of human behavior in the largest economies in the world.
- We use a simpler contrived example for now…
| Marginal Tax Rate | Single Taxable Income | Married Filing Jointly or Qualified Widow(er) Taxable Income | Married Filing Separately Taxable Income | Head of Household Taxable Income |
|---|---|---|---|---|
| 10% | $0 – $9,275 | $0 – $18,550 | $0 – $9,275 | $0 – $13,250 |
| 15% | $9,276 – $37,650 | $18,551 – $75,300 | $9,276 – $37,650 | $13,251 – $50,400 |
| 25% | $37,651 – $91,150 | $75,301 – $151,900 | $37,651 – $75,950 | $50,401 – $130,150 |
| 28% | $91,151 – $190,150 | $151,901 – $231,450 | $75,951 – $115,725 | $130,151 – $210,800 |
| 33% | $190,151 – $413,350 | $231,451 – $413,350 | $115,726 – $206,675 | $210,801 – $413,350 |
| 35% | $413,351 – $415,050 | $413,351 – $466,950 | $206,676 – $233,475 | $413,351 – $441,000 |
| 39.6% | $415,051+ | $466,951+ | $233,476+ | $441,001+ |
How to solve?
- Think about how you would solve such a problem.
- What steps would you take?
- What would making solving it hard?
- Keep track of details?
- Performing the arithmetic?
- Anything else?
- Python, in my view, is a way to solve these problems.
Python in action
- My preferred way to do calculation as an experienced Python user is writing code:
- Mathematical expression \[ \begin{cases} 9x^2 + 5 & x < 4 \\ 9 & 4 \leq x \leq 8 \\ 2 - x & x > 8 \end{cases} \]
- Python expression (code)
- I write
x * xfor \(x^2\) because it’s non-obvious how to write “squared” yet.
Arithmetic Operations
- In Python, we can write many of the same arithmetic operations we use in our math and science classes.
Evaluating Expressions
Recall
\[ \begin{cases} 9x^2 + 5 & x < 4 \\ 9 & 4 \leq x \leq 8 \\ 2 - x & x > 8 \end{cases} \] \[ \{-1, 4, 5, 8, 11\} \]
- This is still quite tedious and annoying!
(In)equality testing
- Like
+or-which we use to calculate numbers… - We can use
<or>to calculate inequalities. - Specifically, we see whether an inequality is
TrueorFalse
Accomodating Keyboards
- Some things aren’t super easy to type.
- I don’t have a “\(\leq\)” key on my keyboard.
- Combine with
=as<=for “less than or equal” or “\(\leq\)”
- We can “chain” inequalities as well - one after another.
A note
- We always put the equal sign
=second.
- A (somewhat confusing) error if we try
=>
File "<stdin>", line 1
4 => 5
^
SyntaxError: cannot assign to literal= and ==
New Topics
- We have now touch on two new topics:
TrueandFalse- Expressions which don’t evaluate to a number
- Called “booleans”
- “Assignment”
- Associated with the
=sign - Different from inequality testing!
- Associated with the
- We’ll explore both!
Booleans
- Sometimes, a Python expression is a numerical value.
- But it doesn’t have to be!
If
- Booleans are mostly useful for writing
ifstatements.- These are multiline expressions in Python.
- To see the result of multi-line expression, we have to print the result…
Piecewise
- We can see the immediate use of this in a piecewise function!
Else
- Oftentimes, we use
ifwithelse
Elif
- If we have more than two options, we can place a special
elifin the middle.
- By the way, it is extremely obnoxious to manually type
2in 5 different places
Assignment
- We can also assign variables!
- I call this single-equals assignment
- Use a single equals sign
=and some variable name, likex
Double equals equality
- I call it single-equals assignment because sometimes we check if a variable is precisely equal to some value.
- That is somewhat confusingly done with
==double equals equality test.
Double ** Exponentation
- Doubling is used in a few other cases.
- Double asterisk
**is exponentiation.
- We note that
^- sometimes used for exponentiation - is something different (and confusing!) in Python.
- Always use
**
Piecewise
- We can finally write the piecewise expression!
Code Reuse
Recycle
- It is still extremely tedious to either:
- Copy-paste, or
- Type more than once.
- Also very annoying/difficult to write them many lines without error!
- We introduce the
defkeyword to definefunctions - A way to reuse code we have already written.
def
- We recall we use
printin a multi-line.
def+ some function name +(+ some variable name +):
- “Call” functions the same way we call
print- with parens.
Metaphor
defis like asking a friend for something.- “Hey can you add
xandxtogether?” printis how your friend tells you the result.- “Can you print out the result of
x + xfor me?”
return
- It is more common to use
returnthanprintin a function. - When we call a function, we can think of it as expression with some value.
- That value is defined by the value that is return.
Example
- Suppose an interest rate doubles our savings every ~7 years.
- How much savings in 14 years?
Use return
- Now,
double(savings)becomes equal tox + x
- Or even
Piecewise
- We can finally write the piecewise function (was: expression)!
- Function: use many times in many expressions
- Expression: evaluate once and get one answer
Iteration
Tedium
- This is still quite tedious.
Sets
- This is a set:
\[ \{-1, 4, 5, 8, 11\} \]
- A set is an unordered collection of elements.
- In this case, elements are integers - whole numbers.
- Python can also recognize sets!
Type
- To be sure that is, in fact, a set, we use the helpful built-in
type()to ask Python.
- We have also worked with integers.
- And booleans -
TrueandFalsevalues.
Loops
- With a set, do something to each element
- In Python, use a
forloop:- The
forkeyword - The name to refer to an element, like
eorx - The
inkeyword - The set/collection (or its variable name)
- The
:colon special character - An indented new line of code
- The
Example
- for element in collection
- do thing
- What do you see?
Example
- What if we just try to print the elements?
- Easier to think about!
- What do you see?
Ordering
- Recall: “A set is an unordered collection of elements”.
- We need to put things in order!
- The most common way to do this in Python is with a list.
- Almost the same as sets, just use “boxy brackets”
[]
Example
- for element in collection
- do thing
- What do you see?
Seeing lists
- We can also use lists to print multiple values!
- Very helpful for keeping track of things!
Updating lists
- Each element is a lot like a variable.
- They just have a name and number, use
[] - The initial element is number
0(not 1)
- They just have a name and number, use
- And like variables, we can assign a value with
=
Metaphor
- Think of a list like a block in a neighborhood.
- Perhaps to “400” block or “8000”
- The house on the corner has the block number.
- 400 1st St, 8000 9th Ave
- Latter houses have higher numbers.
- 404 1st St, 8019 9th Ave
Adding lists
- Lists also helpfully support addition with
+
- We often use this to make new lists.
- This pattern also works with numbers!
Careful!
- We cannot add numbers directly to lists
[1] + 2doesn’t work!
- I think of this as adding a house to block without having an address for it.
- Where would the house go?
- To add a house to block, we need to have a new “lot” for the house to be on.
Exercise
Income tax
- Recall the example of a piecewise function:
| Marginal Tax Rate | Single Taxable Income | Married Filing Jointly or Qualified Widow(er) Taxable Income | Married Filing Separately Taxable Income | Head of Household Taxable Income |
|---|---|---|---|---|
| 10% | $0 – $9,275 | $0 – $18,550 | $0 – $9,275 | $0 – $13,250 |
| 15% | $9,276 – $37,650 | $18,551 – $75,300 | $9,276 – $37,650 | $13,251 – $50,400 |
| 25% | $37,651 – $91,150 | $75,301 – $151,900 | $37,651 – $75,950 | $50,401 – $130,150 |
| 28% | $91,151 – $190,150 | $151,901 – $231,450 | $75,951 – $115,725 | $130,151 – $210,800 |
| 33% | $190,151 – $413,350 | $231,451 – $413,350 | $115,726 – $206,675 | $210,801 – $413,350 |
| 35% | $413,351 – $415,050 | $413,351 – $466,950 | $206,676 – $233,475 | $413,351 – $441,000 |
| 39.6% | $415,051+ | $466,951+ | $233,476+ | $441,001+ |
Singles only…
| Rate | From |
|---|---|
| 10% | 0 |
| 15% | 9275 |
| 25% | 37650 |
| 28% | 91150 |
| 33% | 190150 |
| 35% | 413350 |
| 39.6% | 415050 |
Motivating example
- How much would a single making 400k pay?
- 10% on 9275
- 15% on 37650 - 9275
- 25% on 91150 - 37650
- 28% on 190150 - 91150
- 33% on 400000 - 190150
| Rate | From |
|---|---|
| 10% | 0 |
| 15% | 9275 |
| 25% | 37650 |
| 28% | 91150 |
| 33% | 190150 |
| 35% | 413350 |
| 39.6% | 415050 |
Sum it up!
- Watch out for order-of-operations!
Add as you go
- We may set a variable to an expression over that variable
- I think of this as an “old” version of the variable on the right side of the equal sign.
Assign-update
- We may use an “assignment operator”
+=- Reassign a variable based on the result of an arithmetic operation.