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 = "AAPL"                 # a str: text, always in quotes
shares = 10                     # an int: a whole number

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

print(type(price))              # -> <class 'float'>
print(type(ticker))             # -> <class 'str'>
print(type(shares))             # -> <class 'int'>
185.4
AAPL
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
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
shares   = 10

position = price * shares
print(position)                 # -> 1854.0
1854.0

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

aapl_price  = 185.40
aapl_shares = 10

msft_price  = 410.20
msft_shares = 4

nvda_price  = 128.75
nvda_shares = 20

aapl_value = aapl_price * aapl_shares
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
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

NVDA 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 AMZN at 178.30. Add the position to the portfolio above, then print the new total and AMZN’s weight rounded to four decimals.

amzn_price  = 178.30
amzn_shares = 15

amzn_value = amzn_price * amzn_shares
print(amzn_value)                        # -> 2674.5

new_total = total + amzn_value
print(new_total)                         # -> 8744.3
print(round(amzn_value / new_total, 4))  # -> 0.3059