As a parent, planning for your children’s future is one of the most important things you can do.

To ensure they have the best possible start in life, I’ve been looking into ways to invest money to provide them with a lump sum of $70k when they reach 21. But I don’t want to spend that much money at once when they turn 21.

*So what to do?*

In this blog post, I’ll be exploring how Python helped me figure out a simple investment plan, so I can give my kids a nice nest egg without needing to spend a lot of our (parents’) money at once.

π **Result**: I need to set up a savings plan contributing $90 per month for 21 years in a vehicle earning 9% per year (e.g., S&P500 ETF), so my kids get a $70k nest egg when they start into their own lives.

Here’s the Python code — I’ll explain it in a moment:

import matplotlib.pyplot as plt import numpy as np # Define initial investment value, investment return, and monthly contributions initial_investment = 1000 investment_return = 0.09 monthly_contributions = [30,60,90] num_years = 21 # Create list of portfolio values over time for each savings rate portfolio_values = [] for contribution in monthly_contributions: portfolio = [initial_investment] portfolio_value = initial_investment for i in range(1,num_years-1): portfolio_value = portfolio_value * (1 + investment_return) + contribution * 12 portfolio.append(portfolio_value) portfolio_values.append(portfolio) # Plot portfolio values over time time = np.arange(1,num_years) for i in range(len(monthly_contributions)): plt.plot(time, portfolio_values[i], label='$' + str(monthly_contributions[i]) + '/m') plt.title('Portfolio Value over Time') plt.xlabel('Time (years)') plt.ylabel('Portfolio Value ($)') # Add end value labels for i in range(len(monthly_contributions)): plt.text(num_years-3, portfolio_values[i][-1], '$' + str(int(portfolio_values[i][-1]*1.3))) plt.legend() plt.show()

Result:

This code snippet plots the value of a portfolio for different monthly contributions over a period of 21 years.

You specify the

- initial investment value,
- investment return,
- number of years, and
- monthly contributions.

Then, you create a list to store the portfolio values over time for each monthly contribution.

You calculate the portfolio values by multiplying the previous value of the portfolio by the investment return plus the contribution for each month. You repeat this calculation over the number of years minus one, taking the initial investment into account.

You use Matplotlib to plot the portfolio values. The x-axis is the time in years and the y-axis is the portfolio value in dollars.

π **Recommended Tutorial**: A Video Guide on Matplotlib

You add a title and labels to the plot. You also add the end value for each of the portfolios to the plot with a text label. Finally, you display the plot.

## Action Steps

- Copy the code into your own Python script.
- Figure out your (base) investment goals for your kids or yourself. What do you need to accomplish? What would be your dream outcome?
- Change the input values until you’re happy with your results.

You can check out our Finxter Academy course on Matplotlib to learn all you need to use plotting and data visualization in Python.

This is part of our **100 practical Python projects** series. Subscribe here.

By the way, Finxter **Mark** came up with this clever script that is kind of a generalization of the previous code snippet. Very nicely done!

# A program from the 100 Projects for 2023 Series at Finxter.com # 6 January 2023 import matplotlib.pyplot as plt import numpy as np '''Get the initial values from the user for the plots. Initial investment, return rate, contributions (number of them for plot) ''' # Define initial investment value, investment return, and monthly contributions initial_investment = int(input('Initial Invest: ')) investment_return = float(input('Return Rate (ex 0.03): ')) # creating an empty list monthly_contributions = [] # number of elements as input n = int(input("Enter number of different monthly contribution amounts: ")) # iterating till the range for i in range(0, n): # grab the elements and append, let user know which one we are on of the set ele = int(input('Contribution amount ' + str(i+1) + ': ')) monthly_contributions.append(ele) # adding the element num_years = int(input('Years: ')) # Create list of portfolio values over time for each savings rate portfolio_values = [] for contribution in monthly_contributions: portfolio = [initial_investment] portfolio_value = initial_investment for i in range(1,num_years-1): portfolio_value = portfolio_value * (1 + investment_return) + contribution * 12 portfolio.append(portfolio_value) portfolio_values.append(portfolio) # Plot portfolio values over time time = np.arange(1,num_years) for i in range(len(monthly_contributions)): plt.plot(time, portfolio_values[i], label='$' + str(monthly_contributions[i]) + '/m') plt.title('Portfolio Value over Time') plt.xlabel('Time (years)') plt.ylabel('Portfolio Value ($)') # Add end value labels for i in range(len(monthly_contributions)): plt.text(num_years-2, portfolio_values[i][-1], '$' + str(int(portfolio_values[i][-1]*1.3))) plt.legend() plt.show()

Here’s an example run of the flexible scripts with three kids investing $1000 in a lump sum (each) and $50, $100, and $200 monthly contributions for 20 years:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.