• <ins id="rwosr"><acronym id="rwosr"></acronym></ins>
      <kbd id="rwosr"></kbd>

      1. <tr id="rwosr"></tr>
        <code id="rwosr"></code> <output id="rwosr"><track id="rwosr"></track></output>

        <output id="rwosr"><track id="rwosr"></track></output>
        <sup id="rwosr"><track id="rwosr"></track></sup>
        <tr id="rwosr"><nobr id="rwosr"><delect id="rwosr"></delect></nobr></tr>

        <output id="rwosr"><track id="rwosr"></track></output>
        400-650-7353
        您所在的位置:首頁 > IT干貨資料 > python > 【Python基礎知識】常用內建模塊-math和random

        【Python基礎知識】常用內建模塊-math和random

        • 發布: 優就業it培訓
        • 來源:
        • 2021-10-29 14:40:29
        • 閱讀()
        • 分享
        • 手機端入口

        math 模塊

        math模塊中定義了一些數學函數,用于進行數學計算。此外,該模塊中還定義了兩個數學常數:

        1. >>> import math   # 導入math模塊,以下示例都將省略這行代碼 
        2. >>> math.pi   # 圓周率π 
        3. 3.141592653589793 
        4. >>> math.e   # 數學常數e 
        5. 2.718281828459045 

        數學模塊提供了兩個角度轉換函數,degrees()函數用于將弧度轉換成角度,radians()函數用于將角度轉換成弧度:

        1. >>> math.radians(30)   # 將角度30°轉換成弧度 
        2. 0.5235987755982988 
        3. >>> math.degrees(math.pi/2)   # 將弧度π/2轉換成角度 
        4. 90.0 
        5. >>> math.degrees(math.pi/3)   # 將弧度π/3轉換成角度 
        6. 59.99999999999999 

        數學模塊還提供了用于計算給定角度的各種三角函數,如sin()、cos()、tan()等,這些三角函數需要以弧度作為參數:

        1. >>> math.sin(math.radians(30))   # 計算sin30°,結果約等于0.5 
        2. 0.49999999999999994 
        3. >>> math.sin(math.radians(90))   # 計算sin90°,結果等于1.0 
        4. 1.0 
        5. >>> math.cos(math.radians(90))   # 計算cos90°,結果約等于0 
        6. 6.123233995736766e-17 
        7. >>> math.cos(math.radians(0))   # 計算cos0°,結果等于1.0 
        8. 1.0 
        9. >>> math.tan(math.radians(45))   # 計算tan45°,結果約等于1 
        10. 0.9999999999999999 

        log()函數用于計算給定數字的自然對數,自然對數以e為底數;log10()函數用于計算給定數字的以10為底的對數;log2()函數用于計算給定數字的以2為底的對數:

        1. >>> math.log(10
        2. 2.302585092994046 
        3. >>> math.log(math.e) 
        4. 1.0 
        5. >>> math.log10(10
        6. 1.0 
        7. >>> math.log2(4
        8. 2.0 
        9. >>> math.log2(1024
        10. 10.0 

        factorial()函數用于計算給定數字的階乘:

        1. >>> math.factorial(3)   # 3的階乘為3 * 2 * 1 
        2. 6 
        3. >>> math.factorial(10
        4. 3628800 

        pow(x, y)函數用于接收兩個浮點數作為參數,計算x的y次冪:

        1. >>> math.pow(33)   # 計算3的3次冪 
        2. 27.0 
        3. >>> math.pow(28)   # 計算2的8次冪 
        4. 256.0 
        5. >>> math.pow(34)   # 計算3的4次冪 
        6. 81.0 

        sqrt()函數用于計算給定數字的平方根:

        1. >>> math.sqrt(100)   # 計算100的平方根 
        2. 10.0 
        3. >>> math.sqrt(16)   # 計算16的平方根 
        4. 4.0 

        ceil()函數用于將給定浮點數向上取整,floor()函數用于將給定浮點數向下取整:

        1. >>> math.ceil(3.4
        2. 4 
        3. >>> math.ceil(3.6
        4. 4 
        5. >>> math.floor(3.4
        6. 3 
        7. >>> math.floor(3.6
        8. 3 

        random模塊

        random模塊中定義了很多隨機函數,用于生成隨機數,或者進行隨機操作。

        random()函數用于產生一個在[0, 1)范圍內的隨機數:

        1. >>> import random   # 導入random模塊,以下示例都將省略這行代碼 
        2. >>> random.random() 
        3. 0.4571616492269954 
        4. >>> random.random() 
        5. 0.15751801783441732 
        6. >>> random.random() 
        7. 0.3304966043254054 

        如果想要生成一個隨機整數,可以使用randint()函數,接收兩個參數,分別是生成整數范圍的最小值和最大值:

        1. >>> random.randint(1100)  # 產生一個1~100的隨機整數 
        2. 52 

        也可以使用列表生成式,通過randint()函數創建一個包含10個1~100的整數的隨機列表:

        1. >>> random_numbers = [random.randint(1100for i in range(10)] 
        2. >>> random_numbers 
        3. [76377988466164871158

        randrange()函數接收三個參數,分別是生成數字的最大值、最小值和步長。例如,可以利用randrange()函數的步長特性來生成1~100的隨機奇數:

        1. >>> random.randrange(11002)   # 由于步長是2,因此生成的數字全部是奇數 
        2. 17 
        3. >>> random.randrange(11002
        4. 77 
        5. >>> random.randrange(11002
        6. 45 
        7. >>> random.randrange(11002
        8. 49 

        choice()函數用于從序列中選擇一個隨機項并返回它:

        1. >>> random.choice('Python')   # 從字符串中隨機選擇一個字母 
        2. 'n' 
        3. >>> random.choice('Python'
        4. 'P' 
        5. >>> students = ['Wang''Zhang''Liu''Li']   # 從列表中隨機選擇一個名字 
        6. >>> random.choice(students) 
        7. 'Liu' 
        8. >>> random.choice(students) 
        9. 'Li' 
        10. >>> random.choice(students) 
        11. 'Liu' 

        shuffle()函數用于將序列中的各個項隨機排序。例如,可以通過shuffle()函數來“洗牌”:

        1. >>> cards = ['紅桃2''梅花k''方片9''黑桃J'
        2. >>> random.shuffle(cards) 
        3. >>> cards 
        4. ['方片9''紅桃2''梅花k''黑桃J'
        5. >>> random.shuffle(cards) 
        6. >>> cards 
        7. ['紅桃2''梅花k''方片9''黑桃J'

         

        文章“【Python基礎知識】常用內建模塊-math和random”已幫助

        更多內容

        >>本文地址:http://www.littlerockbway.com/zhuanye/2021/70620.html

        THE END  

        聲明:本站稿件版權均屬中公教育優就業所有,未經許可不得擅自轉載。

        1 您的年齡

        2 您的學歷

        3 您更想做哪個方向的工作?

        獲取測試結果
        • 大前端大前端
        • 大數據大數據
        • 互聯網營銷互聯網營銷
        • JavaJava
        • Linux云計算Linux
        • Python+人工智能Python
        • 嵌入式物聯網嵌入式
        • 全域電商運營全域電商運營
        • 軟件測試軟件測試
        • 室內設計室內設計
        • 平面設計平面設計
        • 電商設計電商設計
        • 網頁設計網頁設計
        • 全鏈路UI/UE設計UI設計
        • VR/AR游戲開發VR/AR
        • 網絡安全網絡安全
        • 新媒體與短視頻運營新媒體
        • 直播帶貨直播帶貨
        • 智能機器人軟件開發智能機器人
         

        快速通道fast track

        近期開班時間TIME

        两个人在线观看免费播放_国产精品天干天干_亚洲婷婷月色婷婷五月小蛇_男人的好小说全文免费阅读
      2. <ins id="rwosr"><acronym id="rwosr"></acronym></ins>
          <kbd id="rwosr"></kbd>

          1. <tr id="rwosr"></tr>
            <code id="rwosr"></code> <output id="rwosr"><track id="rwosr"></track></output>

            <output id="rwosr"><track id="rwosr"></track></output>
            <sup id="rwosr"><track id="rwosr"></track></sup>
            <tr id="rwosr"><nobr id="rwosr"><delect id="rwosr"></delect></nobr></tr>

            <output id="rwosr"><track id="rwosr"></track></output>
            <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <文本链> <文本链> <文本链> <文本链> <文本链> <文本链>