与其他编程语言一样,Python 模运算符用于找到给定数字的模数。该运算符是一个数学符号,用于执行不同的操作,如(+,-,* /)加法、减法、乘法和除法,以在整数和浮点数形式返回结果。运算符告诉编译器基于给定数字的传递运算符符号执行某些操作。

190.png

模运算符

Python 模运算符是一种内置运算符,通过将第一个数除以第二个数返回余数。它也被称为 Python 模数。在 Python 中,模数表示为百分号(%)符号。因此,它被称为余数运算符。

语法

以下是获取第一个数字除以第二个数字的余数的语法。

Rem = X % Y   

这里 X 和 Y 是两个整数,两者之间使用模数(%)进行除法运算,以获取余数,其中第一个数字(X)被第二个数字(Y)除以。

例如,我们有两个数字,24 和 5。我们可以使用模数或模运算符在数字 24 % 5 之间获取余数。在这里,24 除以 5 返回 4 作为余数,4 作为商。当第一个数字完全被另一个数字除尽,没有余数时,结果将为 0。例如,我们有两个数字,20 和 5。我们可以使用模数或模运算符在数字 20 % 5 之间获取余数。在这里,20 除以 5 返回 0 作为余数,4 作为商。

使用 while 循环获取两个整数数字的模

让我们编写一个程序,使用 Python 中的 while 循环和模数(%)运算符获取两个数字的余数。

Get_rem.py

while True:           # here, if the while condition is true then if block will be executed    
    a = input ('Do you want to continue or not (Y / N)? ')    
    if a.upper() != 'Y':     # here, If the user pass 'Y', the following statement is executed.    
        break    
    a = int(input (' First number is: '))    # here, we are taking the input for first number    
    b = int(input (' Second number is: '))   
     # Here, we are taking the input for second number    
    print("The result after performing modulus operator is: ", a, ' % ', b, ' = ', a % b)   
# Here, we are performing the modulus a % b        
    print("The result after performing modulus operator is:", b, ' % ', a, ' = ', b % a)   
# Here, we are performing the modulus b % a    

输出:

Do you want to continue or not (Y / N)? Y
First number is: 37
Second number is: 5
The result after performing modulus operator is: 37 % 5 = 2
The result after performing modulus operator is: 5 % 37 = 5
Do you want to continue or not (Y / N)? Y
First number is: 37
Second number is: 5
The result after performing modulus operator is: 24 % 5 = 4
The result after performing modulus operator is: 5 % 24 = 5
Do you want to continue or not (Y / N)? Y
First number is: 37
Second number is: 5
The result after performing modulus operator is: 28 % 5 = 3
The result after performing modulus operator is: 5 % 28 = 5

获取两个浮点数的余数

让我们编写一个程序,使用模数运算符在 Python 中找到两个整数数字的余数。

Mod.py

x = float(input ('First number: '))         
# Here, we are taking the input for the first number    
y = float(input (' Second number: '))       
# Here, we are taking the input for the second number    
res = x % y       # Here, we are storing the remainder in a new 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

获取负数的余数

让我们编写一个程序,使用 while 循环和模数(%)运算符在 Python 中找到两个负数的余数。

Mod.py

while True:    
    x = input(' Do you want to continue (Y / N)? ')    
    if x.upper() != 'Y':    
        break    
    # Here, we are taking input two integer number and store it into x and y    
    x = int(input (' First number: '))    
    # Here, we are taking the input for the first number  
    y = int(input (' Second number: '))    
    # Here, we are taking the input for the 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() 函数获取两个浮点数的余数

让我们编写一个程序,使用 Python 中的 fmod() 函数找到两个浮点数的余数。

Fmod.py

import math    # here, we are importing the math package to use fmod() function.    
res = math.fmod(25.5, 5.5)    # here, we are passing the parameters    
print ("Modulus using fmod() is:", res)    
ft = math.fmod(75.5, 15.5)    
print (" Modulus using fmod() is:", ft)    
# Here, we are taking two integers from the user    
x = int( input( "First number is"))  
# Here, we are taking the input for the first number     
y = int (input ("Second number is "))    
# Here, we are taking the input for the second number  
out = math.fmod(x, y)     # here, we are passing 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

使用函数获取 n 个数字的余数

让我们编写一个 Python 程序,使用函数和 for 循环来找到 n 个数字的余数。

getRemainder.py

def getRemainder(n, k):     # here, we are creating a function  
    for i in range(1, n + 1):     # here, we are declaring a for loop  
    # Here, we are storing remainder in the rem variable when i is divided by k number    
        rem = i % k       
        print(i, " % ", k, " = ", rem, sep = " ")       
# Here, the code for use _name_ driver     
if __name__ == "__main__":      
   # Here, we 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 "))    # here, we are defining the divisor       
    # Here, we are calling 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() 函数获取给定数组的余数

让我们编写一个程序来演示 Python 中的 mod() 函数。

Mod_fun.py

import numpy as np      # here, we are importing the numpy package    
x = np.array([40, -25, 28, 35])     # here, we are define the first array    
y = np.array([20, 4, 6, 8])      # here, we are define the second array    
# Here, we are calling the 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]

正如我们在上面的程序中看到的,x 和 y 变量包含数组。之后,我们使用 mod() 函数将 x 和 y 作为数组参数传递,该函数将第一个数组(x)除以第二个数组(y),然后返回数字的余数。

使用 numpy 获取两个数字的余数。

让我们考虑一个程序,从 Python 库中导入 numpy 包,然后使用余数函数来获取 Python 中的余数。

Num.py

import numpy as np      # here, we are importing the numpy package as np  
# Here, we are giving the declaration of the variables with their values    
num = 38        # here, we are initializing the num variable  
num2 = 8         # here, we are initializing the num2 variable  
res = np.remainder(num, num2)     # here, we are using the np.remainder() function    
print("Modulus is", num, " % ", num2, " = ", res)   
# Here, we are displaying the modulus num % num2    

输出:

Modulus is 38 % 8 = 6

Python 模运算符中的异常

在 Python 中,当一个数被零除时,它会抛出一个异常,这个异常被称为 ZeroDivisionError。换句话说,当一个数字可以被零除时,它返回一个异常。因此,如果我们想要从 Python 模运算符中移除异常,除数不能为零。

让我们编写一个程序来演示模运算符中的 Python 异常。

Except.py

x = int(input (' The first number is: '))        
# Here, we are taking the input for the first number  
y = int(input (' The second number is: '))    
# Here, we are taking the input for the second number  
# Here, we are displaying the exception handling    
try:            # here, we are defining the try block    
    print (x, ' % ', y, ' = ', x % y)    
except ZeroDivisionError as err:       # here, we are defining the exception block     
    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.

正如我们在上面的结果中看到的,它显示了 "不能除以零!所以,请更改右操作数的值。"。因此,我们可以说,当我们将第一个数字除以零时,它返回一个异常。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程