400-650-7353
您所在的位置:首頁 > IT干貨資料 > python > 【Python基礎(chǔ)知識】常用內(nèi)建模塊-math和random

【Python基礎(chǔ)知識】常用內(nèi)建模塊-math和random

  • 發(fā)布: 優(yōu)就業(yè)it培訓(xùn)
  • 來源:
  • 2021-10-29 14:40:29
  • 閱讀()
  • 分享
  • 手機端入口

math 模塊

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

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

數(shù)學(xué)模塊提供了兩個角度轉(zhuǎn)換函數(shù),degrees()函數(shù)用于將弧度轉(zhuǎn)換成角度,radians()函數(shù)用于將角度轉(zhuǎn)換成弧度:

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

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

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

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

  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()函數(shù)用于計算給定數(shù)字的階乘:

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

pow(x, y)函數(shù)用于接收兩個浮點數(shù)作為參數(shù),計算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()函數(shù)用于計算給定數(shù)字的平方根:

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

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

  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模塊中定義了很多隨機函數(shù),用于生成隨機數(shù),或者進行隨機操作。

random()函數(shù)用于產(chǎn)生一個在[0, 1)范圍內(nèi)的隨機數(shù):

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

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

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

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

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

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

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

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

  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()函數(shù)用于將序列中的各個項隨機排序。例如,可以通過shuffle()函數(shù)來“洗牌”:

  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基礎(chǔ)知識】常用內(nèi)建模塊-math和random”已幫助

更多內(nèi)容

>>本文地址:http://nfbqydst.cn/zhuanye/2021/70620.html

THE END  

聲明:本站稿件版權(quán)均屬中公教育優(yōu)就業(yè)所有,未經(jīng)許可不得擅自轉(zhuǎn)載。

1 您的年齡

2 您的學(xué)歷

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

獲取測試結(jié)果
  • 大前端大前端
  • 大數(shù)據(jù)大數(shù)據(jù)
  • 互聯(lián)網(wǎng)營銷互聯(lián)網(wǎng)營銷
  • JavaJava
  • Linux云計算Linux
  • Python+人工智能Python
  • 嵌入式物聯(lián)網(wǎng)嵌入式
  • 全域電商運營全域電商運營
  • 軟件測試軟件測試
  • 室內(nèi)設(shè)計室內(nèi)設(shè)計
  • 平面設(shè)計平面設(shè)計
  • 電商設(shè)計電商設(shè)計
  • 網(wǎng)頁設(shè)計網(wǎng)頁設(shè)計
  • 全鏈路UI/UE設(shè)計UI設(shè)計
  • VR/AR游戲開發(fā)VR/AR
  • 網(wǎng)絡(luò)安全網(wǎng)絡(luò)安全
  • 新媒體與短視頻運營新媒體
  • 直播帶貨直播帶貨
  • 智能機器人軟件開發(fā)智能機器人
 

快速通道fast track

近期開班時間TIME