黄视频网站在线免费观看-黄视频网站在线看-黄视频网站在线观看-黄视频网站免费看-黄视频网站免费观看-黄视频网站免费

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

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

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

Python模數(shù)運算符

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

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

模數(shù)算子

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

語法

下面是用第一個數(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 作為商。當?shù)谝粋€數(shù)可以被另一個數(shù)完全整除而不留下任何余數(shù)時,結(jié)果將是 0。

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

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

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

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

得到負數(shù)的模數(shù)

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

在 Python 中,當一個數(shù)被零除時,它會拋出一個異常,這個異常被稱為零除錯誤。換句話說,當一個數(shù)可以被一個零除數(shù)整除時,它會返回一個異常。因此,如果我們想從 Python 模數(shù)運算符中移除異常,除數(shù)不應該為零。

讓我們編寫一個程序來演示模數(shù)運算符中的 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ù)的值。因此,我們可以說,當我們將第一個數(shù)除以 0 時,它會返回一個異常。

tags: python教程
聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
10年以上業(yè)內(nèi)強師集結(jié),手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內(nèi)將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
二级片在线观看| 一级片免费在线观看视频| 日本久久久久久久 97久久精品一区二区三区 狠狠色噜噜狠狠狠狠97 日日干综合 五月天婷婷在线观看高清 九色福利视频 | 色综合久久天天综合| 成人免费一级毛片在线播放视频| 国产精品1024永久免费视频| 国产视频久久久久| 香蕉视频久久| 日韩在线观看视频黄| 九九国产| 国产麻豆精品hdvideoss| 久久国产精品永久免费网站| 欧美一级视频免费观看| 亚州视频一区二区| 四虎影视库国产精品一区| 成人影院一区二区三区| 欧美a级成人淫片免费看| 国产高清在线精品一区二区| 国产一区二区精品久| 国产网站在线| 日韩男人天堂| 黄色福利片| 精品国产一区二区三区久| a级黄色毛片免费播放视频| 99久久精品国产片| 国产一区二区精品尤物| 成人免费一级毛片在线播放视频| 国产视频在线免费观看| 高清一级做a爱过程不卡视频| 高清一级毛片一本到免费观看| 色综合久久久久综合体桃花网| 精品国产香蕉在线播出| 久久福利影视| 九九国产| 精品久久久久久免费影院| 日韩男人天堂| 深夜做爰性大片中文| 国产麻豆精品hdvideoss| 国产视频网站在线观看| 精品视频在线观看一区二区| 国产不卡在线播放| 久久久久久久男人的天堂| 国产亚洲精品aaa大片| 日韩专区第一页| 久久成人性色生活片| 欧美a级片视频| 色综合久久手机在线| 91麻豆精品国产自产在线| 可以在线看黄的网站| 亚欧乱色一区二区三区| 亚洲www美色| 国产精品1024永久免费视频 | 日韩在线观看网站| 国产不卡福利| 国产一区二区高清视频| 青青青草影院 | 中文字幕Aⅴ资源网| 午夜在线影院| 日韩一级黄色大片| 欧美爱色| 国产伦精品一区二区三区无广告| 国产网站在线| 国产伦精品一区二区三区无广告| 精品国产亚洲人成在线| 久久久久久久男人的天堂| 午夜久久网| 国产精品自拍一区| 国产精品12| 欧美国产日韩精品| 99色吧| a级毛片免费观看网站| 久久国产精品只做精品| 天天做人人爱夜夜爽2020毛片| 台湾毛片| 日本免费区| 久久国产精品自由自在| 亚洲精品久久久中文字| 欧美另类videosbestsex高清| 色综合久久天天综合| 国产一级生活片| 香蕉视频三级| 精品在线观看一区| 午夜在线观看视频免费 成人| 欧美国产日韩精品| 91麻豆精品国产片在线观看| 欧美爱爱动态| 国产91精品一区二区| 国产伦久视频免费观看 视频| 国产麻豆精品hdvideoss| 欧美激情一区二区三区视频 | 国产不卡精品一区二区三区| 日本特黄特黄aaaaa大片| 成人免费一级纶理片| 国产麻豆精品免费密入口| 国产一区二区精品| 99热热久久| 中文字幕一区二区三区精彩视频| 二级片在线观看| 欧美激情在线精品video| 91麻豆国产福利精品| 精品国产香蕉在线播出| 四虎影视库国产精品一区| 毛片高清| 青青久久精品| 国产视频一区二区在线观看| 成人影院久久久久久影院| 韩国毛片免费| 国产国语对白一级毛片| 精品久久久久久中文字幕一区| 成人免费观看男女羞羞视频| 欧美电影免费| 免费一级片在线| 欧美夜夜骑 青草视频在线观看完整版 久久精品99无色码中文字幕 欧美日韩一区二区在线观看视频 欧美中文字幕在线视频 www.99精品 香蕉视频久久 | 高清一级毛片一本到免费观看| 成人在免费观看视频国产| 国产不卡在线播放| 在线观看导航| 久久国产精品永久免费网站| 九九久久国产精品大片| 99热视热频这里只有精品| 久久99中文字幕| 国产不卡在线播放| 日韩avdvd| 二级片在线观看| 色综合久久天天综线观看| 成人免费福利片在线观看| 亚欧乱色一区二区三区| 午夜在线观看视频免费 成人| 可以免费看毛片的网站| 日本久久久久久久 97久久精品一区二区三区 狠狠色噜噜狠狠狠狠97 日日干综合 五月天婷婷在线观看高清 九色福利视频 | 欧美大片一区| 欧美夜夜骑 青草视频在线观看完整版 久久精品99无色码中文字幕 欧美日韩一区二区在线观看视频 欧美中文字幕在线视频 www.99精品 香蕉视频久久 | 青草国产在线观看| 色综合久久天天综合观看| 亚洲精品影院久久久久久| 国产原创视频在线| 午夜激情视频在线播放| 国产视频一区二区三区四区| 国产网站在线| 精品国产一区二区三区久久久蜜臀 | 欧美一级视| 青青青草影院 | 成人免费网站久久久| 国产一区二区精品| 久久国产精品只做精品| 成人a大片高清在线观看| 麻豆网站在线看| 精品视频在线观看视频免费视频 | 欧美电影免费看大全| 日本免费看视频| 国产a视频| 国产网站麻豆精品视频| 999久久久免费精品国产牛牛| 亚洲 欧美 成人日韩| 亚洲 男人 天堂| 欧美另类videosbestsex| 91麻豆精品国产综合久久久| 亚洲wwwwww| 青青久久精品| 欧美另类videosbestsex高清| 成人免费高清视频| 日本免费乱人伦在线观看 | 99色视频在线| 国产韩国精品一区二区三区| 欧美日本国产| 日韩免费片| 国产伦精品一区二区三区在线观看| 欧美日本韩国| 青青青草影院| 成人影院一区二区三区| 精品毛片视频| 九九国产| 91麻豆精品国产片在线观看| 欧美激情一区二区三区在线| 青草国产在线| 99色视频在线观看| 97视频免费在线| 日本特黄特色aaa大片免费| 欧美激情一区二区三区在线| 青草国产在线| 精品国产三级a| 久久精品大片| 精品久久久久久中文字幕一区| 午夜欧美成人久久久久久| 亚洲精品影院一区二区| 免费毛片播放| 国产a网| 国产成人啪精品| 天天做日日干| 免费一级生活片| 精品国产一区二区三区国产馆| 成人高清护士在线播放| 四虎久久精品国产| 欧美一级视| 成人免费观看男女羞羞视频| 欧美爱色| 国产91视频网| 成人免费观看视频| 青草国产在线观看| 日本久久久久久久 97久久精品一区二区三区 狠狠色噜噜狠狠狠狠97 日日干综合 五月天婷婷在线观看高清 九色福利视频 |