Run the same lines once for every item in a list, and add the results up as you go.
A for loop runs the same block of code once for every item in a list. You write the block once and Python repeats it, handing you one item at a time.
I show four things here: the loop variable, indentation, zip for walking two lists in step, and a running total.
Step 1. Print each close
Three closing prices. One loop.
closes = [185.4, 187.2, 184.9] # three closing prices, oldest firstfor c in closes: # c becomes 185.4, then 187.2, then 184.9print("close:", c)# -> close: 185.4# -> close: 187.2# -> close: 184.9
close: 185.4
close: 187.2
close: 184.9
c is the loop variable. The name is yours to pick. The line for c in closes: ends in a colon, and the indented line below it is the body: the part Python repeats.
Indentation decides what belongs to the loop. Move a line out and it runs once, after the loop has finished.
for c in closes:print("inside :", c) # indented, so runs three timesprint("outside:", c) # not indented, so runs once, after the loop# -> inside : 185.4# -> inside : 187.2# -> inside : 184.9# -> outside: 184.9
The last line prints 184.9 because c keeps the value it held on the final pass.
Step 2. Two lists in step
zip walks two lists together and hands you one item from each.
dates = ["Mon", "Tue", "Wed"] # one label per close, same ordercloses = [185.4, 187.2, 184.9] # both lists must be the same lengthfor d, c inzip(dates, closes): # first pass: d = "Mon", c = 185.4print(d, c)# -> Mon 185.4# -> Tue 187.2# -> Wed 184.9
Mon 185.4
Tue 187.2
Wed 184.9
Two loop variables now, d and c, filled position by position.
Step 3. Add things up as you go
To accumulate a total, create it before the loop and add to it inside.
total =0.0# created before the loopfor c in closes: total += c # shorthand for: total = total + cprint("running total:", round(total, 1))# -> running total: 185.4# -> running total: 372.6# -> running total: 557.5print(total /len(closes)) # -> 185.83333333333334
Move total = 0.0 inside the loop and it is reset on every pass:
for c in closes: total =0.0# reset on every pass total += c # so only the last close ever survivesprint(total) # -> 184.9
184.9
Step 4. Value a portfolio
Three holdings, each with a price and a share count. The value of one holding is price times shares.
tickers = ["AAA", "CCC", "DDD"]prices = [185.40, 410.20, 128.75] # price per share, same order as tickersshares = [10, 4, 20] # shares held, same order againtotal =0.0# the portfolio value builds up in herefor tkr, px, n inzip(tickers, prices, shares): # zip handles three lists too value = px * n # one holding: price times shares total += value # running sum across holdingsprint(tkr, value)# -> AAA 1854.0# -> CCC 1640.8# -> DDD 2575.0print("total", total) # -> total 6069.8
AAA 1854.0
CCC 1640.8
DDD 2575.0
total 6069.8
Add a fourth stock to all three lists and the loop values it too. Nothing else changes.
Your turn
Print each holding’s weight: its value divided by the portfolio total. The total has to exist before you can divide by it, so this takes two passes.
TipShow answer
total =sum(px * n for px, n inzip(prices, shares)) # a one-line version of the loop abovefor tkr, px, n inzip(tickers, prices, shares):print(tkr, round(px * n / total, 3))# -> AAA 0.305# -> CCC 0.27# -> DDD 0.424
Before rounding, the weights sum to 1, so each one is the share of the portfolio held in that stock.