久久精品国产亚洲高清|精品日韩中文乱码在线|亚洲va中文字幕无码久|伊人久久综合狼伊人久久|亚洲不卡av不卡一区二区|精品久久久久久久蜜臀AV|国产精品19久久久久久不卡|国产男女猛烈视频在线观看麻豆

    1. <style id="76ofp"></style>

      <style id="76ofp"></style>
      <rt id="76ofp"></rt>
      <form id="76ofp"><optgroup id="76ofp"></optgroup></form>
      1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

        手機(jī)站
        千鋒教育

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

        千鋒教育

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

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

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

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

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

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

        像其他編程語言一樣,Python 模數(shù)運(yùn)算符做同樣的工作來找到給定數(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)算符,通過將第一個(gè)數(shù)字除以第二個(gè)數(shù)字來返回剩余的數(shù)字。它也被稱為巨蟒模。在 Python 中,模數(shù)符號(hào)表示為百分比( % )符號(hào)。因此,它被稱為余數(shù)運(yùn)算符。

        語法

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

        
        Rem = X % Y 
        

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

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

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

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

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

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

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

        讓我們編寫一個(gè) 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ù)組的模

        讓我們編寫一個(gè)程序來演示 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 庫中導(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ò)誤。換句話說,當(dāng)一個(gè)數(shù)可以被一個(gè)零除數(shù)整除時(shí),它會(huì)返回一個(gè)異常。因此,如果我們想從 Python 模數(shù)運(yùn)算符中移除異常,除數(shù)不應(yīng)該為零。

        讓我們編寫一個(gè)程序來演示模數(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ù)的值。因此,我們可以說,當(dāng)我們將第一個(gè)數(shù)除以 0 時(shí),它會(huì)返回一個(gè)異常。

        tags: python教程
        聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
        10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
        請(qǐng)您保持通訊暢通,專屬學(xué)習(xí)老師24小時(shí)內(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
        為什么Hadoop是用Java實(shí)現(xiàn)的?

        一、跨平臺(tái)能力多平臺(tái)運(yùn)行:Java的“一次編寫,到處運(yùn)行”理念,使得Hadoop能在各種操作系統(tǒng)和硬件上運(yùn)行,不需要特定的調(diào)整。廣泛應(yīng)用:這一特...詳情>>

        2023-10-15 16:51:37
        ECU是什么?

        1、ECU的基本定義與作用ECU,全稱為電子控制單元,是一種專門用于控制汽車各個(gè)系統(tǒng)的微處理器控制系統(tǒng)。通過接收傳感器的信號(hào)并轉(zhuǎn)換成控制指令...詳情>>

        2023-10-15 16:29:54
        什么是SOA?

        1、SOA的基本概念與核心原則SOA是一種使軟件組件通過網(wǎng)絡(luò)進(jìn)行互操作的架構(gòu)模式。核心原則包括:可發(fā)現(xiàn)的服務(wù):服務(wù)應(yīng)容易發(fā)現(xiàn)和理解。松耦合:...詳情>>

        2023-10-15 16:19:32
        什么是內(nèi)存池?

        1、內(nèi)存池的基本概念內(nèi)存池是一種內(nèi)存管理策略,旨在優(yōu)化內(nèi)存分配性能和減少碎片化。通過將內(nèi)存分配到大小固定的池中,應(yīng)用程序可以快速、高效...詳情>>

        2023-10-15 16:16:15
        ci構(gòu)建與編譯的區(qū)別是什么?

        一、功能與目的構(gòu)建(Build): 構(gòu)建是將源代碼轉(zhuǎn)化為可執(zhí)行代碼的過程,它包括編譯、鏈接、打包等一系列步驟。構(gòu)建不僅僅局限于編譯,還可能涉...詳情>>

        2023-10-15 15:57:11
        快速通道
        嘉禾县| 江陵县| 海原县| 留坝县| 临朐县| 南丹县| 二手房| 司法| 白山市| 威海市| 宽甸| 肥城市| 乐山市| 奇台县| 固安县| 麦盖提县| 临潭县| 垦利县| 武川县| 黄大仙区| 乐东| 南川市| 塘沽区| 乌兰察布市| 阳谷县| 乌拉特前旗| 色达县| 巴塘县| 伽师县| 涿鹿县| 大庆市| 遂昌县| 尼玛县| 泌阳县| 平陆县| 兴山县| 资兴市| 清流县| 台前县| 侯马市| 进贤县|