开始使用免费开始使用

投资回报预测

在 Python 中,您可以像在 MATLAB 中一样使用 = 给变量赋值。不过不需要使用分号!

# 圆的面积
area = 3.14 * (radius ** 2)

与 MATLAB 一样,圆括号中的表达式会先计算。这有助于确保先把半径平方,再乘以 $\pi$。

在本练习中,您将通过"赚钱"来检验 Python 的运算顺序!您向一个投资账户存入了一笔资金,需要预测 5 年后的账户价值。该账户的参数如下:

  • 初始投资:$1000
  • 年回报率:4.1%

您可以用下面的公式计算在 years 年之后账户的最终价值:

\( final = deposit (1 + rate)^{years} \)

本练习是课程的一部分

面向 MATLAB 用户的 Python

查看课程

练习说明

  • 将初始存款金额、年回报率和投资年数分别赋值给变量 depositannual_rateyears
  • 计算投资 years 年后的账户最终金额,并保存到变量 final
  • 计算最终金额与初始存款之间的差额,并保存到 gain
  • 打印账户的收益。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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)
编辑并运行代码