Forecasting an investment
You can assign a value to a variable in Python with =
, just like MATLAB. But you don't need to use semi-colons!
# Area of a circle
area = 3.14 * (radius ** 2)
Like in MATLAB, expressions inside of parentheses are evaluated first, which is useful to ensure that the radius is squared before it is multiplied by \(\pi\).
In this exercise, you'll test out of the order of operations in Python by making some money! You've made a deposit in an investment account, and need to forecast the account's value after five years. The account has the following parameters:
- Initial investment: $1000
- Annual rate of return: 4.1%
You can calculate the final value of the account after X number of years
with the following formula:
\( final = deposit (1 + rate)^{years} \)
This exercise is part of the course
Python for MATLAB Users
Exercise instructions
- Assign the amount of the initial deposit, the annual rate of return, and the number of years of the investment to the variables
deposit
,annual_rate
, andyears
, respectively. - Calculate the final value of the account after
years
of investment and save it to the variablefinal
. - Calculate the difference between the final value and deposited amount and save it to
gain
. - Print the gain in the account.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create variables with the initial deposit, annual_rate, and number of years
deposit =
annual_rate =
years =
# Calculate the final value of the account & save it to the variable final
____ = ____
# Define gain as the difference between the final value and deposited value
# Print the gain, in dollars
print(gain)