Final summary from my notes, 2022:

Hello everyone! Before class:¶

  1. In GH Desktop: Fetch/pull your class notes repo and the lecture repo.
  2. Suggested: Open the module notes file on one side of your Jupyterlab screen and the two files above on the other side of your screen.

💪 💪 Time to power up 💪 💪

Today¶

  1. Preliminary syntax rules: indentation
  2. For loops (*)
  3. HOT TIPS: ABCD and building code
  4. If-elif-else (*)
  5. Bugs
  6. If time: Gitignore

Indentation¶

  • Python has strong opinions about indentations
  • Indentations at the beginning of lines are not "up to you"
  • Indentations indicate a "block" of code that is run as a unit.
In [4]:
x = 7
if x < 5:                   # this is false, obviously,
    z = x+2                 # so this never runs and 
    print('I am not here.') # this doesn't print
if x < 5:          # this is false, obviously,
    z = x+2        # so this never runs  
print('I am here') # BUT! BUT! this DOES print
I am here

(Omg, this slide violates a golden rule. Which?)

For loops¶

Syntax:

for <name> in <iterable object>:     # you must use the colon!
    <do some stuff, refer to "name"> # you must indent lines in the loop
    <do some more stuff if you want> # you must indent lines in the loop 
    <do even more stuff>             # you must indent lines in the loop

Here is a silly (non-functional) example of a loop calculating the cost of a portfolio.

Prof: write on paper all explicit steps this code does

# pre-loop stuff 
stonks = ['GM','F','TSLA','MSFT']
prices = {'GM':10,'F':20,'TSLA':1000,'MSFT':500}
tot = 0 # will add up cost in this variable

for stonk in stonks:
    price = get_price_of(stonk) # do some stuff
    tot = tot + price           # then increment!

Looping notes:

  1. Choose a good <name>! Perhaps: the singular of what you're looping over? state in states, stonk in stonks, etc
  2. Especially early on: Print the objects within your loop
    • You might be surprised at what the object is
    • Textbook has another worked out example
    • Next slide...

Huge Tip 1: ABCD¶

  • 8.D of the Chap 2 golden rules: Always Be Checking Your Data
  • Echoes 1.8.2 "debugging" trick: PRINT YOUR DATA AND OBJECTS OFTEN!
  • Here: print the objects in your loops!
In [2]:
# finding a total by incrementing

mynumbers = [2,4,8]       # some input
tot = 0                   # build answer here  
for num in mynumbers:
    print('on num: ',num) # ABCD using the tip I just gave
    tot = tot + num       # increment
    print('tot now:',tot)        
print(tot)

# Doesn't this algo seem like a silly way to get total?
# Q1: Is there a quicker way to compute that?
# Q2: Then why is that a useful construct? 
on num:  2
tot now: 2
on num:  4
tot now: 6
on num:  8
tot now: 14
14

Huge Tip 2: How to build loops¶

  1. Write the pseudo code

Huge Tip 2: How to build loops¶

  1. Write the pseudo code

pseudo

Meme courtesy of Lana Butorovic

Huge Tip 2: How to build loops¶

  1. Write the pseudo code

  2. Write skeleton for loop: works on ONE element, and prints out progress within

    for stonk in stonks[0]: # stonks[0] is only the first ele
         print(stonk) # ABCD!
    
  3. Now, write the code that works on single element.

    for stonk in stonks[0]:
         print(stonk)
         # start working here...
         # when you make a new obj, like "price" --> ABCD!
    
  4. Then try multiple parts of the object, e.g. [0:3]

Exercises¶

These are the assets (copy from handouts/Py Day 2 assets.py)

stonks = ['GM','F','TSLA','MSFT']
prices = {'GM':10,'F':20,'TSLA':1000,'MSFT':500}
shares = {'GM':4,'F':3,'TSLA':2,'MSFT':2}
weights = {'GM':0.35,'F':0.4,'TSLA':0.3,'MSFT':-0.05} # not typo
industry = {'GM':'auto','F':'auto','TSLA':'auto','MSFT':'tech'}

Solve these using a for-loop:

  1. What is the portfolio cost if you buy one share each?
  2. What is the portfolio cost if you buy the number of shares indicated in the shares dictionary above?
  3. If you have 10,000 and use the portfolio weights from weights, how many shares of each firm will you buy? Output the answer HOWEVER you want (easiest option: print as you find out)

Autoformatting tip: After writing your code, CTRL+SHIFT+F if you installed the extensions to J-Lab

If, elif, else¶

Syntax:

if <condition 1>:                         # you must use the colon!
     <do some stuff if condition is true> # remember to indent things inside the if
elif <condition 2>:                       # you must use the colon!
    <do stuff if condition 1 is false but condition 2 is true>
else:
    <if neither condition 1 nor 2 are true, do this>

Comments:

  • You can include zero or as many elif code blocks as you want
  • You can omit the else block entirely
  • Whatever is in <condition> must evaluate to True or False or 1 (true) or 0 (false)
  • A <condition> can be complex:
if (Poor and TaxRateAtOrBelowNegative10) or (MiddleClass and TaxRateAtOrBelow5) or (Rich and TaxRateBelow15):
    start_audit()

Exercises¶

These are the assets:

stonks = ['GM','F','TSLA','MSFT']
prices = {'GM':10,'F':20,'TSLA':1000,'MSFT':500}
shares = {'GM':4,'F':3,'TSLA':2,'MSFT':2}
weights = {'GM':0.35,'F':0.4,'TSLA':0.3,'MSFT':-0.05} # not typo
industry = {'GM':'auto','F':'auto','TSLA':'auto','MSFT':'tech'}
  1. Write a if statement that will give the stock price if the stonk is an auto firm.
  2. Write a for loop that gets the price of a portfolio of 1 share each for the auto firms.

Debugging¶

We will run into many, many varied issues this semester. It's infeasible to avoid them, plus overcoming them is actually important to the learning process!

Tools to fix AND avoid are discussed in Chapter 1.8, but in summary:

  1. For errors that give error codes, READ THE ERROR CODES! (prof demo)
  2. ABCD, or: print(), a LOT (while developing code)
  3. Reset the kernel, clear output, and rerun from scratch
  4. Google/stack overflow the error code
  5. More help

The worst bugs are the type that let your code keep "working". Just lurking there, ruining your work...

To avoid those lurcking errors...

ABCD!¶

These are usually often due to either

  • planning errors (you thought you could do A->B->C, but C is impossible after B)
  • Or because you did something that didn't do exactly what you thought

The latter happens a TON with big data projects, and we will talk about techniques to reduce such errors.

But I have ONE BIG, WEIRD TRICK and let me tell, you, BUGS HATE IT:

Seriously...

Look at your data and objects OFTEN! Print, print, print!¶

This isn't even a "debugging" point per se.

  • You know a 6 is a 6.
  • In big datasets, it's easy to make rather large changes without knowing exactly what you have done, ... or not done ... , if you don't see into the object.
  • Are you sure that object is exactly what you think it is, containing exactly what you think it does? Thus, the print statement and other ways of "glancing into" datasets are crucial. Even when you've all become pythonic pros, you should tend towards examining your objects "too much".

Apologies for repeating that so much!¶

(I violated rule 7.B (DRY) to make a point.)

But printing too much does help while developing code

When you're done developing it, and it works: delete the extraneous print() commands or comment them out

Gitignore¶

  • Working on projecs will create files
  • You don't want all these to end up on github.com
  • Examples:
    • .DS_Store (a temporary file generated by macOS)
    • ipynb checkpoint files (created by j-lab)
    • temporary data files

ENTER .gitignore: It tells GH-D to stop syncing certain files and folders from your comp to github.com

How to use .gitignore:

  1. When you start a project, add a "template" version of a gitignore
    • Can copy from the python one here
  2. When you're basically done, you're ready to clean up your repo. Chapter 1.11 has specific, but the "pseudocode" is:
    1. Look at the repo online and note any files you don't want to have there
    2. Add the file or folder names to the .gitignore
    3. Delete the files/folders off the repo online

Future changes to those files/folders should be ignored by GH-D

If we have time, I'll do a small demo

If not, read chapter 1.11

Summary¶

Big things:

  • ABCD x10,000
  • Write psuedo code
  • For loops, and building them
  • If statements
  • Bugs

This weekend¶

Good luck with assignment 1! Due Sunday!

  • Commit and push some (any!) change today - if you can't see your work on github.com, then contact Julio ASAP.
  • Highly encourage a good read, will help on the assignment AND the rest of class
    • Chapter 1.7 (python)
    • Chapter 2 (all of it!)

Shutting down¶

And at the end of each class,

  • Restart your kernal and rerun your code
  • save all open code files,
  • close JLab (and the terminal running it)
  • commit and push your Class Notes repo
  • open the GitHub.com version of the repo