0% found this document useful (0 votes)
2 views2 pages

Python Lesson5 Collections

Uploaded by

ryan sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Lesson5 Collections

Uploaded by

ryan sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Phase 1 – Lesson 5: Collections (Lists, Tuples,

Dicts, Sets)

Collections let you store multiple values. In finance, you'll use them for price series, trade records,
positions by ticker, and unique symbol sets. This lesson stays loop-free; we'll revisit automation with
loops in Phase 2.

1) Lists – ordered & mutable


Use for: sequences of changing values (e.g., recent prices).

prices = [100.5, 101.2, 102.8, 99.7]


prices[0] → 100.5
prices[-1] → 99.7
[Link](105.3) # add
prices[1] = 101.9 # modify

2) Tuples – ordered & immutable (in depth)


Use for: records you don't want to change (e.g., a trade fill).

trade = ("TSLA", 50, 720.50) # (ticker, shares, buy_price)


trade[0] → "TSLA"
# trade[0] = "AAPL" ■ (can't modify a tuple)

Unpacking:
ticker, shares, buy_price = trade # creates variables on the spot
# ticker == "TSLA", shares == 50, buy_price == 720.50

Why tuples? Safety: accidental edits to historical records are prevented. Many functions also
return tuples so you can unpack multiple values at once.

3) Dictionaries – key → value mapping


Use for: mapping tickers to positions, prices, metadata.

portfolio = {"AAPL": 50, "TSLA": 20, "MSFT": 10}


prices = {"AAPL": 150.5, "TSLA": 720.2, "MSFT": 310.1}

portfolio["AAPL"] → 50
portfolio["TSLA"] += 5 # increase shares

4) Sets – unique, unordered


Use for: unique tickers across sources, fast membership checks.

tickers = {"AAPL", "TSLA", "MSFT", "TSLA"}


# {'AAPL', 'TSLA', 'MSFT'} (duplicates removed)
"AAPL" in tickers → True
5) Mini-Project (no loops yet): Portfolio Report
Given:
portfolio = {"AAPL": 10, "TSLA": 5, "MSFT": 12}
prices = {"AAPL": 150.5, "TSLA": 720.2, "MSFT": 310.1}

Compute values manually (we'll automate with loops in Phase 2):

aapl_value = portfolio["AAPL"] * prices["AAPL"]


tsla_value = portfolio["TSLA"] * prices["TSLA"]
msft_value = portfolio["MSFT"] * prices["MSFT"]
total_value = aapl_value + tsla_value + msft_value

print(f"AAPL: {portfolio['AAPL']} shares, Value ${aapl_value:,.2f}")


print(f"TSLA: {portfolio['TSLA']} shares, Value ${tsla_value:,.2f}")
print(f"MSFT: {portfolio['MSFT']} shares, Value ${msft_value:,.2f}")
print("------------------------")
print(f"Total Portfolio Value: ${total_value:,.2f}")

Bonus (peek ahead): Tuple-of-tuples portfolio


portfolio_trades = (
("AAPL", 10, 150.0),
("TSLA", 5, 700.0),
("MSFT", 12, 300.0)
)
# We won't loop yet; just note how each trade record is immutable and safely structured.

You might also like