Python教程-Python程序查找复利
在本教程中,我们将学习如何编写Python程序来查找给定值的复利。在继续编写程序之前,让我们首先了解复利的基础知识。
复利
在存款或贷款的本金值中添加利息被称为复利,它也被称为复利。复利基本上是将获得的利息重新投资到本金值,而不是提取或支付它,这将导致下一周期的利息;支付的利息将被添加到本金金额中。
让我们比较复利和简单利息。我们可以看到,在简单利息中,本金值上没有利息的复利,获得的利息在每个周期上都保持不变。
事实: 复利是银行、金融和经济中的标准计算利息的方法。
复利的计算公式:
计算给定本金值(V)的复利的一般数学公式如下:
Total Amount = P(1+r/100)^tp
在上述公式中,我们使用的变量可以描述如下:
总金额 = 本金值 + 获得的复利
P = 本金值
tp = 本金值投资的时间期限
r = 利率
代码实现
到目前为止,我们已经了解了复利的基础知识以及它在我们日常生活中的重要性。我们还了解了计算特定本金值的复利所使用的基本公式。
现在,在本节中,我们将编写一个Python程序来计算给定特定值的复利。为了编写所需的Python程序,我们必须遵循以下步骤:
步骤1: 我们将以用户输入的方式获取本金金额。
步骤2: 然后,我们要求用户设置利率和本金金额投资的时间期限。
步骤3: 在从用户获取这三个所需的变量之后,我们将在程序中使用复利公式,即“总金额= P(1+r/100)^tp”。
步骤4: 我们将结果存储在“Result”变量中。
步骤5: 最后,我们将在程序的输出中打印复利作为结果。
现在,看一下以下Python程序,以更好地理解上述步骤的实施:
示例 -
# Using default function
def compound_rate(PV, CRate, tp):
# Using CI formula
TotalAmount = PV * (pow ((1 + CRate / 100), tp))
# Calculating CI
CInterest = TotalAmount - PV
# Printing CI as result in output
print("Total return value after completion of given time period: ", TotalAmount)
print("Compound interest gained on given amount is", CInterest)
# Taking principal amount value from the user
PV = float(input("Enter the principal amount: "))
CRate = float(input("Enter the rate for compound interest: ")) # taking interest rate value
tp = float(input("Enter the time period for which principal is invested: ")) # taking time period value
# Calling out CI function
compound_rate(PV, CRate, tp)
输出:
Enter the principal amount: 600000
Enter the rate for compound interest: 2.7
Enter the time period for which principal is invested: 20
Total return value after completion of given time period: 1022257.0687807774
Compound interest gained on given amount is 422257.0687807774
解释:
运行上述程序后,我们给出了三个所需的变量,即PV = 600000,CRate = 2.7和tp = 20;我们得到了在给定本金值上获得的复利总值(1022257.0687807774),并在输出中打印了结果。使用这个程序,我们可以计算任何给定金额的复利,无论利率和时间期限如何。