千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

手機(jī)站
千鋒教育

千鋒學(xué)習(xí)站 | 隨時隨地免費(fèi)學(xué)

千鋒教育

掃一掃進(jìn)入千鋒手機(jī)站

領(lǐng)取全套視頻
千鋒教育

關(guān)注千鋒學(xué)習(xí)站小程序
隨時隨地免費(fèi)學(xué)習(xí)課程

當(dāng)前位置:首頁  >  技術(shù)干貨  > Python模數(shù)運(yùn)算符

Python模數(shù)運(yùn)算符

來源:千鋒教育
發(fā)布人:xqq
時間: 2023-07-21 17:06:44 1689930404

像其他編程語言一樣,Python 模數(shù)運(yùn)算符做同樣的工作來找到給定數(shù)字的模數(shù)。運(yùn)算符是一種數(shù)學(xué)符號,用于對給定的兩個數(shù)字執(zhí)行不同的運(yùn)算,如(+、-、* /)加法、減法、乘法和除法,以整數(shù)和浮點(diǎn)數(shù)的形式返回結(jié)果。運(yùn)算符告訴編譯器根據(jù)傳遞給給定數(shù)字的運(yùn)算符符號執(zhí)行某些操作。

模數(shù)算子

Python 模數(shù)運(yùn)算符是一個內(nèi)置運(yùn)算符,通過將第一個數(shù)字除以第二個數(shù)字來返回剩余的數(shù)字。它也被稱為巨蟒模。在 Python 中,模數(shù)符號表示為百分比( % )符號。因此,它被稱為余數(shù)運(yùn)算符。

語法

下面是用第一個數(shù)除以第二個數(shù)得到余數(shù)的語法。


Rem = X % Y 

這里,X 和 Y 是兩個整數(shù),在兩者之間使用模數(shù)(%),得到第一個數(shù)(X)除以第二個數(shù)(Y)的余數(shù)。

例如,我們有兩個數(shù)字,24 和 5。我們可以用 24 % 5 之間的模或模算符得到余數(shù)。這里 24 除以 5,得到 4 作為余數(shù),4 作為商。當(dāng)?shù)谝粋€數(shù)可以被另一個數(shù)完全整除而不留下任何余數(shù)時,結(jié)果將是 0。

使用 While循環(huán)獲取兩個整數(shù)的模

讓我們編寫一個程序,使用 Python 中的 While循環(huán)和 modulus)運(yùn)算符來獲取兩個數(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

得到兩個浮點(diǎn)數(shù)的模

讓我們編寫一個程序,使用 Python 中的模數(shù)運(yùn)算符來查找兩個整數(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ù)

讓我們編寫一個程序,使用 Python 中的 While循環(huán)和 modulus)運(yùn)算符來獲取兩個負(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ù)求兩個數(shù)的模

讓我們編寫一個程序,使用 Python 中的 fmod()函數(shù)來獲取兩個浮點(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個數(shù)的模

讓我們編寫一個 Python 程序,使用函數(shù)和 for循環(huán)來求 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ù)組的模

讓我們編寫一個程序來演示 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]

正如我們在上面的程序中看到的,x 和 y 變量保存數(shù)組。之后,我們使用 mod()函數(shù)傳遞 x 和 y 作為數(shù)組參數(shù),將第一個數(shù)組(x)除以第二個數(shù)組(y),然后返回剩余的數(shù)字。

用 numpy 得到兩個數(shù)的模。

讓我們考慮一個程序,從 Python 庫中導(dǎo)入一個 numpy 包,然后用余數(shù)函數(shù)得到 Python 中的模數(shù)。

號 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)一個數(shù)被零除時,它會拋出一個異常,這個異常被稱為零除錯誤。換句話說,當(dāng)一個數(shù)可以被一個零除數(shù)整除時,它會返回一個異常。因此,如果我們想從 Python 模數(shù)運(yùn)算符中移除異常,除數(shù)不應(yīng)該為零。

讓我們編寫一個程序來演示模數(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.

正如我們在上面的結(jié)果中看到的,它顯示,“不能用零除一個數(shù)!所以,改變右操作數(shù)的值。因此,我們可以說,當(dāng)我們將第一個數(shù)除以 0 時,它會返回一個異常。

tags: python教程
聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
請您保持通訊暢通,專屬學(xué)習(xí)老師24小時內(nèi)將與您1V1溝通
免費(fèi)領(lǐng)取
今日已有369人領(lǐng)取成功
劉同學(xué) 138****2860 剛剛成功領(lǐng)取
王同學(xué) 131****2015 剛剛成功領(lǐng)取
張同學(xué) 133****4652 剛剛成功領(lǐng)取
李同學(xué) 135****8607 剛剛成功領(lǐng)取
楊同學(xué) 132****5667 剛剛成功領(lǐng)取
岳同學(xué) 134****6652 剛剛成功領(lǐng)取
梁同學(xué) 157****2950 剛剛成功領(lǐng)取
劉同學(xué) 189****1015 剛剛成功領(lǐng)取
張同學(xué) 155****4678 剛剛成功領(lǐng)取
鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
董同學(xué) 138****2867 剛剛成功領(lǐng)取
周同學(xué) 136****3602 剛剛成功領(lǐng)取
相關(guān)推薦HOT
快速通道
久久亚洲中文字幕精品一区四,亚洲日本另类欧美一区二区,久久久久久久这里只有免费费精品,高清国产激情视频在线观看
亚洲欧美日韩中文加勒比 | 亚洲欧美偷拍综合图区一区 | 亚洲综合网伊人中文 | 亚洲精品视品在线 | 亚洲AⅤ综合在线欧美一区 亚洲另类sm视频在线观看 | 又爽又黄又无遮挡的视频在线观看 |