CAGR stands for Compound Annual Growth Rate. It is the average annual growth rate of a numeric value (can be an investment, population, or anything else that can be measured in numbers) during a multi-year time period.
The CAGR formula is the following: { [Final Amount] / [Initial Amount] } ^ (1 / Years) – 1
Let’s see how we can use this formula in Python.
See also: How To Calculate CAGR in Excel, How To Calculate CAGR in Google Sheets
def CAGR(): How To Calculate CAGR in Python In 2 Easy Steps
In Python, we can create a simple function using the CAGR formula above to calculate the Compound Annual Growth Rate.
We can create the function using ‘def cagr’ and allow for 3 inputs to the function – initial_value, final_value, and no. of years. Note that the ‘raised to the power of‘ is represented by ‘**‘ in Python.
def cagr(initial_value, final_value, years):
cagr = (final_value / initial_value) ** (1/years) - 1
return cagr
After defining the cagr function, when we need to calculate the CAGR, we can call the function cagr and supply the three numerical input values for initial value, final value, and no. of years. For example, let’s say I want to find the CAGR of a $100,000 investment that grows to $200,000 in 10 years, I will write the following code.
CAGR = cagr(100000,200000,10)
print(CAGR*100)
The function calculates and sends out the CAGR (7.18%) which we can print and view on screen.
def cagr() – how to calculate cagr in python by creating a function
You can cross-verify the answer by using our online CAGR Calculator to make sure the def cagr() function is set up properly.
CAGR Calculator | Reverse CAGR Calculator | Blog Posts
Learn Personal Finance
Check out our new venture multipl !