Tool to Calculate the Net Present Value using Python

Want to calculate the Net Present Value using Python?

If so, I’ll share with you the code to launch the following tool, which can be used to calculate the Net Present Value using Python:

Tool to Calculate the Net Present Value using Python

Tool to Calculate the Net Present Value using Python

Step 1: Copy the code

Copy the code below into Python:

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 800, height = 400)

label0 = tk.Label(root, text='Calculate NPV')
label0.config(font=('helvetica', 14))
canvas1.create_window(400, 40, window=label0)

entry1 = tk.Entry (root, width = 60) 
canvas1.create_window(560, 100, window=entry1)

entry2 = tk.Entry (root, width = 60) 
canvas1.create_window(560, 140, window=entry2)

entry3 = tk.Entry (root, width = 60) 
canvas1.create_window(560, 180, window=entry3)

entry4 = tk.Entry (root, width = 30) 
canvas1.create_window(400, 300, window=entry4)

label1 = tk.Label(root, text='    Initial Investment (Absolute Value):')
label1.config(font=('helvetica', 10))
canvas1.create_window(160, 100, window=label1)

label2 = tk.Label(root, text=' Cash Flow Each Year (Separated by Comma):')
label2.config(font=('helvetica', 10))
canvas1.create_window(200, 140, window=label2)

label3 = tk.Label(root, text='   Discount Rate (%):                     ')
label3.config(font=('helvetica', 10))
canvas1.create_window(160, 180, window=label3)

def calcNPV ():  
    cf0 = float(entry1.get())
    r = float(entry3.get())/100
    cashflows = entry2.get().split(',') #Enter values Separated by Commas

    sum = 0
    for cf in cashflows:
        t = t+1
        pv = float(cf)/(1+r)**t
        sum = sum + pv
    npv = sum - cf0
    label4 = tk.Label(root, text= npv,font=('helvetica', 10, 'bold'),bg='white')
    canvas1.create_window(400, 300, window=label4)
button1 = tk.Button(text='Calculate NPV', command=calcNPV, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 25)
canvas1.create_window(400, 250, window=button1)


Step 2: Run the code

Run the code in Python (you can press F5 to run the Python code). You’ll then see the following display:

Calculate the Net Present Value using Python

In the next section, I’ll review a simple example to demonstrate how you can use the tool to calculate the NPV using Python.

Example of Calculating the Net Present Value using Python

Let’s suppose that you have an opportunity to invest in a project where:

  • The initial investment is $1300
  • The project is expected to generate:
    • $500 in year-1
    • $750 in year-2
    • $800 in year-3
  • The discount rate is 7%

What is the NPV of the project? Should you then invest in the project?

To get the NPV using the tool, simply type the values in the entry boxes. Then, click on the green button to calculate the NPV:

Calculate NPV using Python

You’ll then get the NPV of 475.407:

Tool to Calculate the Net Present Value using Python

Since the NPV is positive, you should invest in the project.