14
Thinking Machines
$ and a flashing cursorPS C:\Users\calvin>
PS stands for “powershell” - more latter.C:\Users\calvin is the name of a folder - more latter> is the prompt, with a flashing cursor.In the following examples, I remove line numbers to denote they are not Python code snippets.
pythonpython3Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>>>>python at the command line9 * -1 * -1 + 5 within PythonPS C:\Users\calvin> python
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 9 * -1 * -1 + 5
14
>>>9 * -1 * -1 + 5 is 14Scientists often use the following:
numpy (numerical Python),matplotlib (a suite of plotting tools),scipy (scientific Python), andsympy (symbolic Python).We’ll get to these.
\[ f(x) = \begin{cases} 9x^2 + 5 & x < 4 \\ 9 & 4 \leq x \leq 8 \\ 2 - x & x > 8 \end{cases} \]
| 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+ |
x * x for \(x^2\) because it’s non-obvious how to write “squared” yet.\[ \begin{cases} 9x^2 + 5 & x < 4 \\ 9 & 4 \leq x \leq 8 \\ 2 - x & x > 8 \end{cases} \] \[ \{-1, 4, 5, 8, 11\} \]
+ or - which we use to calculate numbers…< or > to calculate inequalities.True or False= as <= for “less than or equal” or “\(\leq\)”= second.=>= and ==True and False
= signif statements.
if with elseelif in the middle.2 in 5 different places= and some variable name, like x== double equals equality test.** is exponentiation.^ - sometimes used for exponentiation - is something different (and confusing!) in Python.**def keyword to define functionsdefprint in a multi-line.def + some function name + ( + some variable name + ):print - with parens.def is like asking a friend for something.x and x together?”print is how your friend tells you the result.x + x for me?”returnreturn than print in a function.double(savings) becomes equal to x + x\[ \{-1, 4, 5, 8, 11\} \]
type() to ask Python.True and False values.for loop:
for keyworde or xin keyword: colon special character[][]0 (not 1)=+[1] + 2 doesn’t work!| 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+ |
| Rate | From |
|---|---|
| 10% | 0 |
| 15% | 9275 |
| 25% | 37650 |
| 28% | 91150 |
| 33% | 190150 |
| 35% | 413350 |
| 39.6% | 415050 |
| Rate | From |
|---|---|
| 10% | 0 |
| 15% | 9275 |
| 25% | 37650 |
| 28% | 91150 |
| 33% | 190150 |
| 35% | 413350 |
| 39.6% | 415050 |
+=
def single_tax(pay):single_tax_rate which returns the percent tax rate at some income level.| Rate | From |
|---|---|
| 10% | 0 |
| 15% | 9275 |
| 25% | 37650 |
| 28% | 91150 |
| 33% | 190150 |
| 35% | 413350 |
| 39.6% | 415050 |
def single_tax(pay):
tax = 0
if pay > 415050:
tax += (pay - 415050) * .396
pay = 415050
if pay > 413350:
tax += (pay - 413350) * .35
pay = 413350
if pay > 190150:
tax += (pay - 190150) * .33
pay = 190150
if pay > 91150:
tax += (pay - 91150) * .28
pay = 91150
if pay > 37650:
tax += (pay - 37650) * .25
pay = 37650
if pay > 9275:
tax += (pay - 9275) * .15
pay = 9275
return tax + pay * .1pay does tax == pay * .35single_tax with loops