💪 💪 Time to power up 💪 💪
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
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:
<name>
! Perhaps: the singular of what you're looping over? state in states, stonk in stonks, etc# 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
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!
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!
Then try multiple parts of the object, e.g. [0:3]
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:
shares
dictionary above?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
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:
elif
code blocks as you wantelse
block entirely<condition>
must evaluate to True or False or 1 (true) or 0 (false)<condition>
can be complex:if (Poor and TaxRateAtOrBelowNegative10) or (MiddleClass and TaxRateAtOrBelow5) or (Rich and TaxRateBelow15):
start_audit()
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'}
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:
The worst bugs are the type that let your code keep "working". Just lurking there, ruining your work...
To avoid those lurcking errors...
These are usually often due to either
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...
This isn't even a "debugging" point per se.
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". (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
ENTER .gitignore: It tells GH-D to stop syncing certain files and folders from your comp to github.com
How to use .gitignore
:
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
Good luck with assignment 1! Due Sunday!
And at the end of each class,