像其他編程語(yǔ)言一樣,Python 模數(shù)運(yùn)算符做同樣的工作來(lái)找到給定數(shù)字的模數(shù)。運(yùn)算符是一種數(shù)學(xué)符號(hào),用于對(duì)給定的兩個(gè)數(shù)字執(zhí)行不同的運(yùn)算,如(+、-、* /)加法、減法、乘法和除法,以整數(shù)和浮點(diǎn)數(shù)的形式返回結(jié)果。運(yùn)算符告訴編譯器根據(jù)傳遞給給定數(shù)字的運(yùn)算符符號(hào)執(zhí)行某些操作。
模數(shù)算子
Python 模數(shù)運(yùn)算符是一個(gè)內(nèi)置運(yùn)算符,通過(guò)將第一個(gè)數(shù)字除以第二個(gè)數(shù)字來(lái)返回剩余的數(shù)字。它也被稱為巨蟒模。在 Python 中,模數(shù)符號(hào)表示為百分比( % )符號(hào)。因此,它被稱為余數(shù)運(yùn)算符。
語(yǔ)法
下面是用第一個(gè)數(shù)除以第二個(gè)數(shù)得到余數(shù)的語(yǔ)法。
Rem = X % Y
這里,X 和 Y 是兩個(gè)整數(shù),在兩者之間使用模數(shù)(%),得到第一個(gè)數(shù)(X)除以第二個(gè)數(shù)(Y)的余數(shù)。
例如,我們有兩個(gè)數(shù)字,24 和 5。我們可以用 24 % 5 之間的模或模算符得到余數(shù)。這里 24 除以 5,得到 4 作為余數(shù),4 作為商。當(dāng)?shù)谝粋€(gè)數(shù)可以被另一個(gè)數(shù)完全整除而不留下任何余數(shù)時(shí),結(jié)果將是 0。
使用 While
循環(huán)獲取兩個(gè)整數(shù)的模
讓我們編寫(xiě)一個(gè)程序,使用 Python 中的 While
循環(huán)和 modulus)運(yùn)算符來(lái)獲取兩個(gè)數(shù)字的余數(shù)。
Get_rem.py
while True: # if the while condition is true if block is executed
a = input ('Do you want to continue or not (Y / N)? ')
if a.upper() != 'Y': # If the user pass 'Y', the following statement is executed.
break
a = int(input (' First number is: ')) # first number
b = int(input (' Second number is: ')) # second number
print(a, ' % ', b, ' = ', a % b) # perform a % b
print(b, ' % ', a, ' = ', b % a) # perform b % a
輸出:
Do you want to continue or not (Y / N)? Y
First number is: 37
Second number is: 5
37 % 5 = 2
5 % 37 = 5
Do you want to continue or not (Y / N)? Y
First number is: 37
Second number is: 5
24 % 5 = 4
5 % 24 = 5
Do you want to continue or not (Y / N)? Y
First number is: 37
Second number is: 5
28 % 5 = 3
5 % 28 = 5
得到兩個(gè)浮點(diǎn)數(shù)的模
讓我們編寫(xiě)一個(gè)程序,使用 Python 中的模數(shù)運(yùn)算符來(lái)查找兩個(gè)整數(shù)的余數(shù)。
修改 py
x = float(input ('First number: '))
y = float(input (' Second number: '))
res = x % y # store the remainder in res variable
print("Modulus of two float number is: ", x, "%", y, " = ", res, sep = " ")
輸出:
First number: 40.5
Second number: 20.5
Modulus of two float number is: 40.5 % 20.5 = 20.0
得到負(fù)數(shù)的模數(shù)
讓我們編寫(xiě)一個(gè)程序,使用 Python 中的 While
循環(huán)和 modulus)運(yùn)算符來(lái)獲取兩個(gè)負(fù)數(shù)的余數(shù)。
修改 py
while True:
x = input(' Do you want to continue (Y / N)? ')
if x.upper() != 'Y':
break
# input two integer number and store it into x and y
x = int(input (' First number: '))
y = int(input (' Second number: '))
print("Modulus of negative number is: ", x, "%", y, " = ", x % y, sep = " ")
print("Modulus of negative number is: ", y, "%", x, " = ", y % x, sep = " ")
輸出:
First number: -10
Second number: 3
Modulus of negative number is: -10 % 3 = 2
Modulus of negative number is: 3 % -10 = -7
Do you want to continue (Y / N)? N
用 fmod()函數(shù)求兩個(gè)數(shù)的模
讓我們編寫(xiě)一個(gè)程序,使用 Python 中的 fmod()函數(shù)來(lái)獲取兩個(gè)浮點(diǎn)數(shù)的余數(shù)。
Fmod.py
import math # import math package to use fmod() function.
res = math.fmod(25.5, 5.5) # pass the parameters
print ("Modulus using fmod() is:", res)
ft = math.fmod(75.5, 15.5)
print (" Modulus using fmod() is:", ft)
# take two integer from the user
x = int( input( "First number is"))
y = int (input ("Second number is "))
out = math.fmod(x, y) # pass the parameters
print("Modulus of two numbers using fmod() function is", x, " % ", y, " = ", out)
輸出:
Modulus using fmod() is: 3.5
Modulus using fmod() is: 13.5
First number is 24
Second number is 5
Modulus of two numbers using fmod() function is 24 % 5 = 4.0
用函數(shù)求n
個(gè)數(shù)的模
讓我們編寫(xiě)一個(gè) Python 程序,使用函數(shù)和 for
循環(huán)來(lái)求 n 的模。
get main . py
def getRemainder(n, k):
for i in range(1, n + 1):
# Store remainder in the rem variable when i is divided by k number
rem = i % k
print(i, " % ", k, " = ", rem, sep = " ")
# use _name_ driver code
if __name__ == "__main__":
# define the first number for displaying the number up to desired number.
n = int(input ("Define a number till that you want to display the remainder "))
k = int( input (" Enter the second number ")) # define the divisor
# call the define function
getRemainder(n, k)
輸出:
Define a number till that you want to display the remainder 7
Enter the second number 5
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
7 % 5 = 2
使用 mod()函數(shù)獲取給定數(shù)組的模
讓我們編寫(xiě)一個(gè)程序來(lái)演示 Python 中的 mod()函數(shù)。
Mod_fun.py
import numpy as np # import numpy package
x = np.array([40, -25, 28, 35]) # define first array
y = np.array([20, 4, 6, 8]) # define second array
# call mod() function and pass x and y as the parameter
print("The modulus of the given array is ", np.mod (x, y))
輸出:
The modulus of the given array is [0 3 4 3]
正如我們?cè)谏厦娴某绦蛑锌吹降?,x 和 y 變量保存數(shù)組。之后,我們使用 mod()函數(shù)傳遞 x 和 y 作為數(shù)組參數(shù),將第一個(gè)數(shù)組(x)除以第二個(gè)數(shù)組(y),然后返回剩余的數(shù)字。
用 numpy 得到兩個(gè)數(shù)的模。
讓我們考慮一個(gè)程序,從 Python 庫(kù)中導(dǎo)入一個(gè) numpy 包,然后用余數(shù)函數(shù)得到 Python 中的模數(shù)。
號(hào) py
import numpy as np # import numpy package as np
# declaration of the variables with their values
num = 38
num2 = 8
res = np.remainder(num, num2) # use np.remainder() function
print( "Modulus is", num, " % ", num2, " = ", res) # display modulus num % num2
輸出:
Modulus is 38 % 8 = 6
Python 模數(shù)運(yùn)算符中的異常
在 Python 中,當(dāng)一個(gè)數(shù)被零除時(shí),它會(huì)拋出一個(gè)異常,這個(gè)異常被稱為零除錯(cuò)誤。換句話說(shuō),當(dāng)一個(gè)數(shù)可以被一個(gè)零除數(shù)整除時(shí),它會(huì)返回一個(gè)異常。因此,如果我們想從 Python 模數(shù)運(yùn)算符中移除異常,除數(shù)不應(yīng)該為零。
讓我們編寫(xiě)一個(gè)程序來(lái)演示模數(shù)運(yùn)算符中的 Python 異常。
除了. py
x = int(input (' The first number is: '))
y = int(input (' The second number is: '))
# display the exception handling
try: # define the try
print (x, ' % ', y, ' = ', x % y)
except ZeroDivisionError as err: # define the exception
print ('Cannot divide a number by zero! ' + 'So, change the value of the right operand.' )
輸出:
The first number is: 24
The second number is: 0
Cannot divide a number by zero! So, change the value of the right operand.
正如我們?cè)谏厦娴慕Y(jié)果中看到的,它顯示,“不能用零除一個(gè)數(shù)!所以,改變右操作數(shù)的值。因此,我們可以說(shuō),當(dāng)我們將第一個(gè)數(shù)除以 0 時(shí),它會(huì)返回一個(gè)異常。