Math with Variables

Amongst the above variable types, integer and floating point numbers are a nice place to start because there are a number of straight forward operations that can be applied to these variables. For instance, as shown above, we can add numbers together with +. Subtraction, multiplication, and division work in similar ways:

Operation

Operator

Example

Result

Addition

+

2+3

5

Subtraction

-

5-3

2

Multiplication

*

2*3

6

Division

/

6/3

2

Beyond these four basic arithmatic operations, there are two extra operations to know:

Operation

Operator

Example

Result

Exponentiation

**

3**2

9

Modulus

%

4%3

1

The former, exponentiation, is key to many financial applications (e.g. time value of money). Do not mistake ** and ^! While ^ may look more familiar as an exponentiation symbol, it has an entirely different meaning in Python.

These arithmatic operators perform basic tasks with stored data. For instance, if we have variables corresponding to debt and equity, we can calculate leverage.

debt = 20
equity = 80

leverage = (debt) / (debt+equity)
print(leverage)
0.2

Similarly, if we needed to know the holding period return of an investment of \(\$500\) that earns \(2\%\) per year for \(3\) years, we could use these Python operators to find that out.

The math we want to perform is: $\( \frac{500(1+0.02)^3}{500}-1 \)$

500 * (1+.02)**3  / 500 - 1
0.06120800000000015

One important thing to note is that we can only nest operations inside of parentheses. In textbooks, you may see mathematical formulas that make use of curly brackets, \(\{\) and \(\}\), or square brackets, \([\) and \(]\). For example \(x = \big[\big(\frac{1}{3}\big)^2 + 5\big]^{1/2}\) makes use of square brackets. In Python, square and curly brackets have special meanings and can’t be used to nest mathematical expressions. The example equation given here is translated into Python as:

((1/3)**2 + 5)**(1/2)
2.260776661041756

Concept check: Suppose that you anticipate a payment of \(\$100\) two years from now. The annual discount rate is \(5\%\). Calculate the present value of this payment.

The basic arithmatic operators work pretty much as you might expect. You should be careful to remember that exponentiation is done with the double asterisk (**), but otherwise this operation should be familiar as well.

The modulus operation is less common, and, when writing financial analytics programs in Python, there’s only one common use case for this operation. That use case concerns loops, a topic that will be covered in a later notebook. The modulus operator gives you the remainder from division. Let’s look at it in action:

print(3 % 2) # 3/2 has a remainder of 1
print(4 % 2) # 4/2 has a remainder of 0
print(2 % 3) # 2/3 has a remainder of 2
1
0
2

Tests of (In)Equality

We can compare the relative value of two number variables (either integers or floating points or a combination of the two types).

For example, define two variables, debt_longterm and debt_shortterm, that store information about a company’s long term and short term debt, respectively.

debt_longterm = 1000
debt_shortterm = 2000

We can check for whether long term debt exceeds short term debt with the > character. This test returns a Boolean value (True or False).

debt_longterm > debt_shortterm
False

As you might expect, we can also use < and = characters for other tests. For instance:

debt_longterm <= debt_shortterm
True

We have to be careful with tests of equality, however. Remember, we used = to tell Python what information a variable should store. For example, x=7 tells Python that the variable x should store the number seven. Thus, if we want to test for whether the information stored in x equals seven, we type

x == 7

with a double == to denote that we’re testing for equality.

x = 8
y = 8
x == y
True

Related to the <, >, and = characters is the word in. In Python, the word in has a special meaning, so you can’t define a variable with the name in. The word in is used to check for whether a value appears in a list. Use of the word in, like the > or other tests of (in)equality, results in Python returning a Boolean value.

As an example, define tic to be one stock ticker of interest and tickers to be a list of stock tickers.

tic = 'AAPL'
tickers = ['AAPL','MSFT','AMD','NVDA','INTC']

We can check for whether the value stored in tic appears in the list of items held in tickers by using the word in.

tic in tickers
True

Concept check: A project is anticipated to pay out \(\$50\) thousand next year and \(\$75\) thousand the year after, after which time the project will terminate. The project will cost \(\$100\) thousand now to initiate. The discount rate is \(5\%\). In one line of code, estimate the NPV of the project. In a second line of code, test whether this value is greater than zero so that the printout from executing the code block is the statement True or False.

Conditionals

Conditionals specify that a certain bit of code should run conditional on a statement being true or false.

Like user-written functions, the block of code that runs conditionally is indented.

In the example below, we define information about a firm’s current assets and current liabilities. Then we calculate the current ratio to see if the firm has enough current assets to cover its current liabilities. The code incorporates a rule of thumb and tells the user that a current ratio above \(1.5\) is okay, a current ratio below \(1.5\) but above \(1\) is risky, and a current ratio below \(1\) is dangerous.

current_assets = 100
current_liabilities = 60
current_ratio = current_assets / current_liabilities

print(current_ratio)
if current_ratio > 1.5:
    print('Firm is okay!')
elif current_ratio <= 1.5 and current_ratio > 1:
    print('Firm looks risky')
else:
    print('Danger!  Firm needs some cash')
1.6666666666666667
Firm is okay!