首页 > 编程知识 正文

Python 程序:执行算术运算

时间:2023-11-24 15:33:37 阅读:308780 作者:BFAX

写一个 Python 程序,用一个实例对数值执行算术运算。

执行算术运算的 Python 程序

这个 python 程序允许用户输入两个数据类型为 float 的数值。接下来,我们将使用这两个值来执行算术运算,如加法、减法、乘法、指数、模数和除法。

# Python Program to Perform Arithmetic Operations

num1 = float(input(" Please Enter the First Value Number 1: "))
num2 = float(input(" Please Enter the Second Value Number 2: "))

# Add Two Numbers
add = num1 + num2

# Subtracting num2 from num1
sub = num1 - num2

# Multiply num1 with num2
multi = num1 * num2

# Divide num1 by num2
div = num1 / num2

# Modulus of num1 and num2
mod = num1 % num2

# Exponent of num1 and num2
expo = num1 ** num2

print("The Sum of {0} and {1} = {2}".format(num1, num2, add))
print("The Subtraction of {0} from {1} = {2}".format(num2, num1, sub))
print("The Multiplication of {0} and {1} = {2}".format(num1, num2, multi))
print("The Division of {0} and {1} = {2}".format(num1, num2, div))
print("The Modulus of {0} and {1} = {2}".format(num1, num2, mod))
print("The Exponent Value of {0} and {1} = {2}".format(num1, num2, expo))

对于这个 Python 程序进行算术运算的例子,我们将 num1 指定为 10,num2 为 3。请参考 python 程序页面查看其余程序。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。