当前分类:300例题>>正文

Python示例:使用函数执行算术运算

来源:互联网   更新时间:2023年6月18日  

Python 示例

写一个 Python示例,用函数做算术计算。在这个 python 示例中,我们为算术计算定义了几个独立的函数,如加法、减法、除法、乘法、模数和指数。

def addition(num1, num2):
    return num1 + num2

def subtraction(num1, num2):
    return num1 - num2

def multiplication(num1, num2):
    return num1 * num2

def division(num1, num2):
    return num1 / num2

def floordivision(num1, num2):
    return num1 // num2

def modulus(num1, num2):
    return num1 % num2

def exponent(num1, num2):
    return num1 ** num2

num1 = int(input("Enter the First Value  = "))
num2 = int(input("Enter the Second Value = "))

print("The Addition       = {0}".format(addition(num1, num2)))
print("The Subtraction    = {0}".format(subtraction(num1, num2)))
print("The Multiplication = {0}".format(multiplication(num1, num2)))
print("The Division       = {0}".format(division(num1, num2)))
print("The Floor Division = {0}".format(floordivision(num1, num2)))
print("The Modulus        = {0}".format(modulus(num1, num2)))
print("The Exponent       = {0}".format(exponent(num1, num2)))

本文固定链接:https://6yhj.com/leku-p-4305.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: urllib