Python 模数运算符

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

模数算子

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

语法

下面是用第一个数除以第二个数得到余数的语法。


Rem = X % Y 

这里,X 和 Y 是两个整数,在两者之间使用模数(%),得到第一个数(X)除以第二个数(Y)的余数。

例如,我们有两个数字,24 和 5。我们可以用 24 % 5 之间的模或模算符得到余数。这里 24 除以 5,得到 4 作为余数,4 作为商。当第一个数可以被另一个数完全整除而不留下任何余数时,结果将是 0。

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

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

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

得到两个浮点数的模

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

修改 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

得到负数的模数

让我们编写一个程序,使用 Python 中的 While循环和 modulus)运算符来获取两个负数的余数。

修改 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()函数求两个数的模

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

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

用函数求n个数的模

让我们编写一个 Python 程序,使用函数和 for循环来求 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()函数获取给定数组的模

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

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]

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

用 numpy 得到两个数的模。

让我们考虑一个程序,从 Python 库中导入一个 numpy 包,然后用余数函数得到 Python 中的模数。

号 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 模数运算符中的异常

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

让我们编写一个程序来演示模数运算符中的 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.

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


本文链接:https://my.lmcjl.com/post/13273.html

展开阅读全文

4 评论

留下您的评论.