CommencerCommencer gratuitement

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} \)

Cet exercice fait partie du cours

Python for MATLAB Users

Afficher le cours

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, and years, respectively.
  • Calculate the final value of the account after years of investment and save it to the variable final.
  • Calculate the difference between the final value and deposited amount and save it to gain.
  • Print the gain in the account.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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)
Modifier et exécuter le code