Get Started

Discounting cash flows

You can use numpy's net present value function numpy.npv(rate, values) to calculate the net present value of a series of cash flows. You can create these cash flows by using a numpy.array([...]) of values.

Compute the NPV of the same cash flows from the following project, but assuming different discount rates:

Year Cash Flow
1 $100
2 $100
3 $100
4 $100
5 $100

This is a part of the course

“Introduction to Financial Concepts in Python”

View Course

Exercise instructions

  • Calculate the net present value of the investment with cash_flows at a discount rate of 3% per year, and assign it to investment_1.
  • Repeat the process with a discount rate of 5% per year, and assign it to investment_2.
  • Repeat the process with a discount rate of 7% per year, and assign it to investment_3.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

import numpy as np

# Predefined array of cash flows
cash_flows = np.array([100, 100, 100, 100, 100])

# Calculate investment_1
investment_1 = np.npv(rate=____, values=____)
print("Investment 1's net present value is $" + str(round(investment_1, 2)) + " in today's dollars")

# Calculate investment_2
investment_2 = np.npv(rate=____, values=____)
print("Investment 2's net present value is $" + str(round(investment_2, 2)) + " in today's dollars")

# Calculate investment_3
investment_3 = np.npv(rate=____, values=____)
print("Investment 3's net present value is $" + str(round(investment_3, 2)) + " in today's dollars")

This exercise is part of the course

Introduction to Financial Concepts in Python

BeginnerSkill Level
4.6+
8 reviews

Using Python and NumPy, learn the most fundamental financial concepts.

Learn about fundamental financial concepts like the time value of money, growth and rate of return, discount factors, depreciation, and inflation.

Exercise 1: Fundamental financial conceptsExercise 2: Growth and rate of returnExercise 3: Compound interestExercise 4: Discount factors and depreciationExercise 5: Present and future valueExercise 6: Present valueExercise 7: Future valueExercise 8: Adjusting future values for inflationExercise 9: Net present value and cash flowsExercise 10: Discounting cash flows
Exercise 11: Initial project costsExercise 12: Diminishing cash flows

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free