1. Balance after each year
Write code Easy ✓ CompletedWrite a function yearly_balances(start, rate, years) that returns a NumPy array with the balance at the end of each year, from year 1 to years, rounded to 2 decimal places. A balance grows by start * (1 + rate) ** year. Do it without a loop: build the years with np.arange and let NumPy do the power for all of them at once.
import numpy as np
def yearly_balances(start, rate, years):
pass
print(yearly_balances(1000, 0.10, 3))np.arange(1, years + 1) gives 1, 2, ..., years. A scalar raised to an array gives an array, so (1 + rate) ** year_numbers does every year in one step.import numpy as np
def yearly_balances(start, rate, years):
year_numbers = np.arange(1, years + 1)
return np.round(start * (1 + rate) ** year_numbers, 2)
print(yearly_balances(1000, 0.10, 3))