網站首頁 編程語言 正文
數學模塊
import math
ceil -- 上取整
對一個數向上取整(進一法),取相鄰最近的兩個整數的最大值。
import math res = math.ceil(4.1) print(res) # 5
floor -- 下取整
對一個數向下取整(退一法),取相鄰最近的兩個整數的最小值。
import math res = math.floor(-3.9) print(res) # -4
四舍五入
將常用的內置函數 -- round。
pow -- 冪運算
計算一個數字的N次方。
import math """ 調用的數學模塊中的方法和內置的主要區別就是內置返回整數,數學模塊返回小數 """ # 數學模塊 res = math.pow(2, 3) print(res) # 8.0 # 內置函數 res = pow(2, 3) print(res) # 8 # 更簡單的方法就是使用 ** 進行冪運算 res = 2 ** 3 print(res) # 8
sqrt -- 開平方運算
import math res = math.sqrt(9) # 結果為浮點型 print(res) # 3.0
fabs -- 絕對值
import math """ 調用的數學模塊中的方法和內置的主要區別就是內置返回整數,數學模塊返回小數 """ # 數學模塊 res = math.fabs(-12341234123) print(res) # 12341234123.0 # 內置函數 res = abs(-12341234123) print(res) #12341234123
modf -- 拆分整數小數
將一個數值拆分為小數和整數兩個部分,組成元組,值為浮點型。
import math res = math.modf(100.666) print(res) # (0.6659999999999968, 100.0)
copysign -- 正負拷貝
將第二個參數的正負狀態拷貝給第一參數。(返回浮點型)
import math res = math.copysign(100, -200) print(res) # -100.0
fsum -- 序列和
將一個容器中的元素進行求和運算(結果為浮點數)
import math lst = [1, 2, 3] res = math.fsum(lst) print(res) # 6.0
pi -- 圓周率常數
import math res = math.pi print(res) # 3.141592653589793
factorial -- 因數
import math # 求5的因數 factor = math.factorial(5) print(factor) # 120
隨機模塊
import random
random -- 獲取 0~~1 之間的小數
random 隨機獲取0 ~ 1之間的小數(左閉右開)0 <= x < 1
import random res = random.random() print(res) # 0.15195915170299268
randrange -- 獲取指定范圍內的整數
語法:rangrange(start, end[, step])
randint -- 獲取指定范圍整數
語法:randint(a, b)
相比 randrange 靈活性低,但是結束值可用
uniform -- 獲取指定范圍內隨機小數(左閉右開)
import random # uniform 獲取指定范圍內的隨機小數(左閉右開) res = random.uniform(1, 3) print(res) # 2.81589512983781 # 因為內置機制的原因(uniform 可以將數值顛倒,并達到相同的效果) res = random.uniform(3, 1) print(res) # 1.4023313207919326
choice -- 隨機獲取序列中的值(多選一)
import random # 可以嘗試使用 randrange 實現 lst = ['A', 'B', 'C', 'D', 'E'] res = random.choice(lst) print(res) # E
sample -- 隨機獲取序列中的值(多選多,返回列表)
語法:sample(poplation, num)
import random lst = ['A', 'B', 'C', 'D', 'E', 'F'] res = random.sample(lst, 1) print(res) # ['F'] res = random.sample(lst, 2) print(res) # ['C', 'A']
shuffle -- 隨機打亂序列中的值(原地址操作)
import random lst = ['A', 'B', 'C', 'D', 'E', 'F'] random.shuffle(lst) print(lst) # ['F', 'D', 'C', 'B', 'E', 'A']
實現隨機驗證碼
import random def getVer(): ver_code = '' # 隨機抽取四個字符 for i in range(4): s_char = chr(random.randrange(97, 123)) b_char = chr(random.randrange(65, 91)) num = str(random.randrange(10)) lst = [s_char, b_char, num] ver_code += random.choice(lst) return ver_code ver = getVer() print(ver) # b4Vq
原文鏈接:https://www.cnblogs.com/msr20666/p/16287516.html
相關推薦
- 2022-06-07 Python?Numpy庫的超詳細教程_python
- 2022-06-25 Python利用format函數實現對齊打印(左對齊、右對齊與居中對齊)_python
- 2022-12-23 C++中關于union的使用方法說明_C 語言
- 2022-07-09 python?查看cpu的核數實現_python
- 2022-07-26 注冊bean有多少種方式
- 2022-08-10 c#實現哈夫曼樹算法_C#教程
- 2022-09-30 Docker容器harbor私有倉庫部署和管理_docker
- 2022-06-02 python套接字socket通信_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支