Python vs Julia: Calculating NPV

Dinithi Weerakoon
The Startup
Published in
5 min readJun 11, 2020
Photo by Chris Ried on Unsplash

As you might have noticed, there are a lot of people talking about which language is better between Python and Julia. Some say that Python is better and others say that Julia is better. However, there are pros and cons in each of them which makes one better in one situation and make the other better in another situation. There are a lot of articles discussing “Python vs Julia”. So, I’m not going to discuss that here.

I come from a Python background. What I observed when I started learning Julia is how similar and easy to learn these basic Julia syntaxes are for a person who knows Python. However, instead of going over each of the data types, conditionals, loops and functions and discussing the similarities and differences of those syntaxes, I will be solving a simple problem in Python and will do the same algorithm in Julia.

The problem that I have chosen for this article is, calculating the Net Present Value(NPV). For those who don’t know what NPV is, I will explain it in simple words.

When you’re doing some kind of an investment, you should know whether that investment is going to give you a profit in return. This return might take few months or years.

Imagine a situation where you have to invest $100,000 for a business. In the first year you get a loss of $20,000, in the second year you get a profit of $50,000 and in the third year you get a profit of $80,000. If you’re just going to add the numbers, it seems like you’re going to get a $10,000 profit for your investment. However, we get that number by ignoring the effect of time on money. In the real world, the value of $100,000 today is higher than the same amount in three years time. As an example, the number of toffees you can buy from a $50 will be higher than the number of toffees that you can buy for $50 in 3 years. Hence, even if it seems like that we are getting a $10,000 profit in return after 3 years, it might not be the actual case.

There’s going to be another term that will be used in this problem and that is the “Discount Rate”. For now, think of it as the rate of loss of value per year. For this problem, the discount rate would be 5%.

The formula to calculate the NPV is,

Cash Flow= income or the expense
r = discount rate
n= time period (number of years from now)

The manual calculation would look like something like this.

As you can see, the real return is not a profit of $15,000, instead it is a loss of $4589.15.

Now let’s see the solution for this in Python. I’ll be using the basic syntaxes in Python for this one.

As the inputs, first line will contain space separated cash flows. These cash flows could be positive or negative to indicate a income and an expense. Discount rate will be given in the second line.

def NPV(CF,r):
npv=0.0
for i in range(len(CF)):
npv+= float(CF[i])/(1+r)**i
return npv
CF=list(map(float,input().split()))
r=float(input())
print(NPV(CF,r))

The same problem can be solved in Julia in the following way.

function NPV(CF,r)
npv=0.0
for i= 1:length(CF)
npv+= CF[i]/(1+r)^(i-1)
end
return npv
end
CF=parse.(Float64,split(readline(), " "))
r=parse(Float64,readline())
println(NPV(CF,r))

Now, I will be going through the similarities and differences of this code one by one.

  1. Getting the Input
    In both Python and Julia, inputs are inserted as a String. Python uses the “input” function and Julia uses the “readline” function. If you insert space separated inputs, the spaces will be considered as characters in both languages.
  2. Splitting the string
    As I have mentioned the space separated inputs are considered as one string. In order to split them, we can use the split function.
    In Python, it will be used as string.split(). If do not provide anything inside the split function, it will consider the space as the string separator.
    In Julia, it will be used as split(string, <string separator>)
  3. Conversion from string to float
    In Python, the split strings will be converted into float and then they will be mapped to a list.
    However in Julia, by placing a “.” between the function and the input parameters, the parsing function will be applied to each of the elements in the Array.
  4. Defining a function
    The “def” keyword in Python has been replaced by “function” keyword in Julia. Additionally, the colon is also not present in Julia. In order to mark the end of the function, the “end” keyword is used.
  5. Differences in for loop
    The first difference that we can notice in Julia for loops, is the way we write the loop iterable. An equal sign is there instead of the “in” keyword and after that the starting point and the ending point will be stated with a colon.
    It should also be noted that in Julia, array indexes starts with 1 instead of 0. Also, the loop consider the end index as well.
    Just like when defining a function, the “end” keyword should be used to inform the program that the loop ended.
  6. Print Function
    The “print” keyword is available in both Python and Julia. However, there’s another keyword called “println” in Julia. There’s a difference between the two of them. The difference between “print” and “println” is that “println” will move the cursor to the next line and “print” will not move the cursor.

This is just a small example to show you the little syntax differences between Python and Julia. I will talk about some more about Julia on another article. Thanks for reading and stay safe.

--

--