Python 内置函数math,random

时间:2024-05-17 14:34:32

内置函数的一些操作

  - math(数学模块)

  - random(随机模块)

- 使用内置函数时注意需要导入


math

- (ceil)向上取整,返回取整数

 # 向上取整,返回向上取整的数
import math print(math.ceil(9.01))
# 执行结果
10 print(math.ceil(9.54))
# 执行结果
10 print(math.ceil(9.99))
# 执行结果
10

- (floor)向下取整,返回整数

 # 向下取整,返回一个向下取整的数
print(math.floor(8.8))
# 执行结果
8 print(math.floor(8.99))
# 执行结果
8 print(math.floor(8.01))

- (keyword)保留系统关键字,不要和关键字重名

 # 查看当前系统保留关键字,不要和关键字重名
import keyword print(keyword.kwlist)
# 执行结果
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

- (round)四舍五入,返回整数

 # 四舍五入,返回一个整数`

 print(round(5.4))
# 执行结果
5 print(round(5.5))
# 执行结果
6 print(round(5.8))
# 执行结果
6
print(round(5.499))
# 执行结果
5 print(round(5.1))
# 执行结果
5

- (sqrt)开方,返回浮点数

 # 开方,返回平方浮点数

 print(math.sqrt(2))
# 执行结果
1.4142135623730951 print(math.sqrt(10))
# 执行结果
3.1622776601683795

- (pow)幂运算,返回整数

 # 幂运算,返回x,y几次方的结果
print(pow(10,3))
# 执行结果
1000 print(pow(10,5))
# 执行结果
100000 print(10**5)
# 执行结果
100000

- (fabs)返回浮点型的绝对值

 # 返回浮点型的绝对值

 print(math.fabs(-2.6))
# 执行结果
2.6 print(math.fabs(-10))
# 执行结果
10.0 print(math.fabs(5))
# 执行结果
5.0

- (abs)系统自带的函数,返回整数绝对值

 # 系统自带的绝对值,返回自己定义类型的数的绝对值
print(abs(5.11))
# 执行结果
5.11 print(abs(-5.11))
# 执行结果
5.11 print(abs(10))
# 执行结果
10 print(abs(0))
# 执行结果
0

- (fsum)返回可迭代的浮点型总和

 # 求和,返回一个可迭代的总和浮点数

 print(math.fsum([22,44,11,23.9]))
# 执行结果
100.9 print(math.fsum([2312,31,435,124,657,123]))
# 执行结果
3682.0

- (sum)系统自带求和,返回自定义总和

 # 求和,返回一个可迭代的总和类型根据自己定义

 print(sum([22,44,11,23]))
# 执行结果
100 print(sum([22,44,11,23.0]))
# 执行结果
100.0

- (modf)将整数和小数分开,返回第一个小数,第二个整数

 # 将整数和小数分开,返回第一个是小数,第二个是整数,都是带有浮点数
print(math.modf(3))
# 执行结果
(0.0, 3.0) print(math.modf(3.5))
# 执行结果
(0.5, 3.0)

- (copysign)将第二个数符号传给第一个数,返回第一个数

 # 将第二个数的符号传给第一个数,以浮点数形式返回第一个数浮点型
print(math.copysign(4,-4))
# 执行结果
-4.0 print(math.copysign(-4,4))
# 执行结果
4.0

random

- (random)0到1之间随机,返回随机数

 # 获取0到1之间的数,返回0到1之间数
print(random.random())
# 执行结果
0.4126590980553493 for i in range(3):
print(random.random())
# 执行结果
0.45733436454027454
0.34427265945970853
0.6586132845875716

- (randint)指定整数范围内随机,返回随机整数

 # 在指定整数之间随机,返回随机整数
print(random.randint(1,10))
# 执行结果
7 for i in range(3):
print(random.randint(1,100))
# 执行结果
14
68
24

- (randrange)指定范围内随机,可以设置间隔距离,返回随机数

 # 指定范围内随机,也可以说设置间隔距离,返回随机数
print(random.randrange(0,100))
# 执行结果
80 print(random.randrange(0,100,5))
# 执行结果
70 for i in range(3):
print(random.randrange(0,100,5))
# 执行结果
70
80
55

- (choice)在指定的列表中随机,返回随机数

 # 在指定列表内随机,返回随机值
print(random.choice(["fs",2,"kz",90]))
# 执行结果
2 l = [10,23,63,123,634,12]
print(random.choice(l))
# 执行结果
634 for i in range(3):
print(random.choice(["fs",2,"kz",90]))
# 执行结果
90
2
90

- (shuffle)将指定列表进行打乱,返回None

 # 指定列表进行打乱,返回None

 l = [24,25,23,135,76,73,42321,57,23]
print(l)
# 执行结果
7.854645612968136 print(random.shuffle(l))
print(l)
# 执行结果
92.92847361436925
11.924828585708383
64.80197839949321

- (uniform)指定范围内随机,返回浮点型随机

 # 指定范围内随机数,返回浮点数
print(random.uniform(1,100))
# 执行结果
7.854645612968136 for i in range(3):
print(random.uniform(1,100))
# 执行结果
92.92847361436925
11.924828585708383
64.80197839949321