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:





















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













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)

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

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)

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

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

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

debt_longterm <= debt_shortterm

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

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

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 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

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