寫一個 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 程序頁面查看其餘程序。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/247559.html