What is a variable?

Name a price, a ticker and a share count, then value a position and a three-stock portfolio.

A variable is a name for a value. You create one with =, putting the name on the left and the value on the right.

Every value has a type. type(x) tells you which one. In this lesson I name three values, check their types, and then use them to price a single position and a portfolio of three holdings.

Step 1. Name a price, a ticker and a share count

Three values, three names. The three types here are float (a number with a decimal point), str (text in quotes) and int (a whole number).

price  = 185.40                 # a float: it has a decimal point
ticker = "AAA"                 # a str: text, always in quotes
shares = 10                     # an int: a whole number

print(price)                    # -> 185.4
print(ticker)                   # -> AAA
print(shares)                   # -> 10

print(type(price))              # -> <class 'float'>
print(type(ticker))             # -> <class 'str'>
print(type(shares))             # -> <class 'int'>
185.4
AAA
10
<class 'float'>
<class 'str'>
<class 'int'>

Python prints 185.4, not 185.40. Python drops the trailing zero.

The type decides what an operator does. * multiplies numbers and repeats text.

print(shares * price)           # -> 1854.0
print(type(shares * price))     # -> <class 'float'>

shares_text = "10"              # the same digits, but as text
print(shares_text * 2)          # -> 1010   <- text repeated, not doubled
print(type(shares_text * 2))    # -> <class 'str'>
1854.0
<class 'float'>
1010
<class 'str'>

Mixing an int and a float gives a float. Repeating a str gives a longer str.

Step 2. Value a position, then a portfolio

A position is worth price times shares. Name the product too.

price    = 185.40               # quoted price per share
shares   = 10                   # how many of them I hold

position = price * shares       # what the holding is worth today
print(position)                 # -> 1854.0
1854.0

A portfolio is the same calculation once per holding, plus a sum.

aapl_price  = 185.40                         # two names per holding: a price
aapl_shares = 10                             # and a share count

msft_price  = 410.20                         # the dearest share of the three
msft_shares = 4

nvda_price  = 128.75                         # the cheapest share
nvda_shares = 20                             # but the largest number held

aapl_value = aapl_price * aapl_shares        # one position value per holding
msft_value = msft_price * msft_shares
nvda_value = nvda_price * nvda_shares

print(aapl_value)                            # -> 1854.0
print(msft_value)                            # -> 1640.8
print(nvda_value)                            # -> 2575.0

total = aapl_value + msft_value + nvda_value # the three positions added up
print(total)                                 # -> 6069.8
1854.0
1640.8
2575.0
6069.8

Divide each holding by the total to get its weight. Weights are fractions of one, so they add to 1.

print(round(aapl_value / total, 4))    # -> 0.3054
print(round(msft_value / total, 4))    # -> 0.2703
print(round(nvda_value / total, 4))    # -> 0.4242
0.3054
0.2703
0.4242

DDD has the lowest price of the three and the largest weight, because I hold the most shares of it.

Your turn

Buy 15 shares of EEE at 178.30. Add the position to the portfolio above, then print the new total and EEE’s weight rounded to four decimals.

amzn_price  = 178.30                     # the new holding
amzn_shares = 15

amzn_value = amzn_price * amzn_shares    # priced the same way as the others
print(amzn_value)                        # -> 2674.5

new_total = total + amzn_value           # total already holds the first three
print(new_total)                         # -> 8744.3
print(round(amzn_value / new_total, 4))  # -> 0.3059