Python标准库之Math,Random模块使用详解
作者:小小垂髫 时间:2021-02-09 22:33:13
数学模块
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
标签:Python,Math,Random
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何控制弹出一个NTLM验证窗口?
2009-12-16 19:01:00
![](https://img.aspxhome.com/file/UploadPic/200912/16/ntlm-36s.gif)
Python 浪漫烟花实现代码全解
2023-11-16 01:24:56
![](https://img.aspxhome.com/file/2023/5/63875_0s.png)
python 如何利用chinese_calendar 获取上一个工作日日期
2022-01-12 12:07:32
![](https://img.aspxhome.com/file/2023/2/78762_0s.png)
asp随机提取access数据库记录的几种方法
2007-09-06 19:42:00
python实现有效的括号判断实例代码
2023-11-24 14:40:34
Tensorflow Summary用法学习笔记
2023-08-12 15:08:53
Python操作Excel神器openpyxl使用教程(超详细!)
2021-02-28 21:58:35
![](https://img.aspxhome.com/file/2023/9/82589_0s.png)
8行代码实现Python文件去重
2023-11-20 14:47:35
![](https://img.aspxhome.com/file/2023/4/64334_0s.jpg)
Python数据结构之队列详解
2023-11-17 14:04:34
![](https://img.aspxhome.com/file/2023/5/76795_0s.png)
解决Python requests 报错方法集锦
2023-05-05 20:21:27
php查询whois信息的方法
2023-09-05 10:47:00
Python 操作 ElasticSearch的完整代码
2021-07-10 17:26:41
![](https://img.aspxhome.com/file/2023/0/79480_0s.png)
Python+Pygame实战之疯狂吃水果游戏的实现
2021-09-17 11:37:58
![](https://img.aspxhome.com/file/2023/0/88040_0s.gif)
Python换行与不换行的输出实例
2022-01-15 13:10:10
sql不常用函数总结以及事务,增加,删除触发器
2012-07-11 16:03:42
将数据从MySQL迁移到 Oracle的注意事项
2008-12-03 15:41:00
深入理解Python内置函数map filter reduce及与列表推导式对比
2022-06-30 21:39:11
Python编程中NotImplementedError的使用方法
2023-06-08 15:40:40
Python获取android设备cpu和内存占用情况
2023-02-25 20:46:47
![](https://img.aspxhome.com/file/2023/3/83733_0s.png)
asp HTTP_X_FORWARDED_FOR和REMOTE_ADDR
2010-07-02 12:33:00