What is a list?

Hold many values under one name, count them, pick them out by position, and add new ones.

A list holds several values in order under one name. You write it with square brackets and commas: ["AAPL", "MSFT"]. A watchlist is a list. So is a run of daily closing prices.

I show four things here: len() to count the items, x[0] to pick one out by position, x[-1] to pick from the end, and .append() to add one more.

Step 1. A watchlist

Three tickers, one name.

tickers = ["AAPL", "MSFT", "NVDA"]

print(tickers)              # -> ['AAPL', 'MSFT', 'NVDA']
print(len(tickers))         # -> 3
print(tickers[0])           # -> AAPL
print(tickers[1])           # -> MSFT
print(tickers[-1])          # -> NVDA
['AAPL', 'MSFT', 'NVDA']
3
AAPL
MSFT
NVDA

Python counts positions from zero. The first item sits at position 0, the second at position 1, and the third at position 2. tickers[3] stops the program with an IndexError.

Negative positions count from the end. tickers[-1] is the last item and tickers[-2] is the one before it.

.append() puts one new item on the end.

tickers.append("AMZN")

print(tickers)              # -> ['AAPL', 'MSFT', 'NVDA', 'AMZN']
print(len(tickers))         # -> 4
print(tickers[-1])          # -> AMZN
['AAPL', 'MSFT', 'NVDA', 'AMZN']
4
AMZN

The list changed in place. I did not write tickers = tickers.append(...), because .append() edits the list and returns nothing.

Step 2. Five closing prices

Here the order is time. Position 0 is the oldest close, and the last position is the most recent.

closes = [185.40, 187.20, 184.90, 188.10, 190.50]

print(len(closes))          # -> 5
print(closes[0])            # -> 185.4
print(closes[-1])           # -> 190.5
print(closes[-2])           # -> 188.1
5
185.4
190.5
188.1

closes[-1] is the latest close. The same item sits at position len(closes) - 1.

print(closes[len(closes) - 1])   # -> 190.5
190.5

Two consecutive closes give one daily return.

latest = closes[-1]
prior  = closes[-2]
ret    = (latest - prior) / prior

print(round(ret, 4))        # -> 0.0128
0.0128

Now a new day arrives and I append it.

closes.append(192.30)

print(closes)               # -> [185.4, 187.2, 184.9, 188.1, 190.5, 192.3]
print(len(closes))          # -> 6
print(closes[-1])           # -> 192.3
[185.4, 187.2, 184.9, 188.1, 190.5, 192.3]
6
192.3

The same two lines now cover the new day, because closes[-1] tracks the end of the list.

ret = (closes[-1] - closes[-2]) / closes[-2]

print(round(ret, 4))        # -> 0.0094
0.0094

Each return comes from two closes next to each other. Lesson 8 turns a whole list of closes into a list of returns.

Your turn

Make two lists that line up by position: prices = [185.40, 410.20, 128.75] and shares = [10, 4, 20]. Using indexing only, print what the last position in the book is worth.

prices = [185.40, 410.20, 128.75]
shares = [10, 4, 20]

print(prices[-1] * shares[-1])   # -> 2575.0

prices[-1] and shares[-1] both point at position 2, so they describe the same holding. Nothing in Python checks the two lists stay lined up. You keep them in step yourself.