内置函数的一些操作
- math(数学模块)
- random(随机模块)
- 使用内置函数时注意需要导入
math
- (ceil)向上取整,返回取整数
1 # 向上取整,返回向上取整的数
2 import math
3
4 print(math.ceil(9.01))
5 # 执行结果
6 10
7
8 print(math.ceil(9.54))
9 # 执行结果
10 10
11
12 print(math.ceil(9.99))
13 # 执行结果
14 10
- (floor)向下取整,返回整数
1 # 向下取整,返回一个向下取整的数
2 print(math.floor(8.8))
3 # 执行结果
4 8
5
6 print(math.floor(8.99))
7 # 执行结果
8 8
9
10 print(math.floor(8.01))
- (keyword)保留系统关键字,不要和关键字重名
1 # 查看当前系统保留关键字,不要和关键字重名
2 import keyword
3
4 print(keyword.kwlist)
5 # 执行结果
6 ['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)四舍五入,返回整数
1 # 四舍五入,返回一个整数`
2
3 print(round(5.4))
4 # 执行结果
5 5
6
7 print(round(5.5))
8 # 执行结果
9 6
10
11 print(round(5.8))
12 # 执行结果
13 6
14 print(round(5.499))
15 # 执行结果
16 5
17
18 print(round(5.1))
19 # 执行结果
20 5
- (sqrt)开方,返回浮点数
1 # 开方,返回平方浮点数
2
3 print(math.sqrt(2))
4 # 执行结果
5 1.4142135623730951
6
7 print(math.sqrt(10))
8 # 执行结果
9 3.1622776601683795
- (pow)幂运算,返回整数
1 # 幂运算,返回x,y几次方的结果
2 print(pow(10,3))
3 # 执行结果
4 1000
5
6 print(pow(10,5))
7 # 执行结果
8 100000
9
10 print(10**5)
11 # 执行结果
12 100000
- (fabs)返回浮点型的绝对值
1 # 返回浮点型的绝对值
2
3 print(math.fabs(-2.6))
4 # 执行结果
5 2.6
6
7 print(math.fabs(-10))
8 # 执行结果
9 10.0
10
11 print(math.fabs(5))
12 # 执行结果
13 5.0
- (abs)系统自带的函数,返回整数绝对值
1 # 系统自带的绝对值,返回自己定义类型的数的绝对值
2 print(abs(5.11))
3 # 执行结果
4 5.11
5
6 print(abs(-5.11))
7 # 执行结果
8 5.11
9
10 print(abs(10))
11 # 执行结果
12 10
13
14 print(abs(0))
15 # 执行结果
16 0
- (fsum)返回可迭代的浮点型总和
1 # 求和,返回一个可迭代的总和浮点数
2
3 print(math.fsum([22,44,11,23.9]))
4 # 执行结果
5 100.9
6
7 print(math.fsum([2312,31,435,124,657,123]))
8 # 执行结果
9 3682.0
- (sum)系统自带求和,返回自定义总和
1 # 求和,返回一个可迭代的总和类型根据自己定义
2
3 print(sum([22,44,11,23]))
4 # 执行结果
5 100
6
7 print(sum([22,44,11,23.0]))
8 # 执行结果
9 100.0
- (modf)将整数和小数分开,返回第一个小数,第二个整数
1 # 将整数和小数分开,返回第一个是小数,第二个是整数,都是带有浮点数
2 print(math.modf(3))
3 # 执行结果
4 (0.0, 3.0)
5
6 print(math.modf(3.5))
7 # 执行结果
8 (0.5, 3.0)
- (copysign)将第二个数符号传给第一个数,返回第一个数
1 # 将第二个数的符号传给第一个数,以浮点数形式返回第一个数浮点型
2 print(math.copysign(4,-4))
3 # 执行结果
4 -4.0
5
6 print(math.copysign(-4,4))
7 # 执行结果
8 4.0
random
- (random)0到1之间随机,返回随机数
1 # 获取0到1之间的数,返回0到1之间数
2 print(random.random())
3 # 执行结果
4 0.4126590980553493
5
6 for i in range(3):
7 print(random.random())
8 # 执行结果
9 0.45733436454027454
10 0.34427265945970853
11 0.6586132845875716
- (randint)指定整数范围内随机,返回随机整数
1 # 在指定整数之间随机,返回随机整数
2 print(random.randint(1,10))
3 # 执行结果
4 7
5
6 for i in range(3):
7 print(random.randint(1,100))
8 # 执行结果
9 14
10 68
11 24
- (randrange)指定范围内随机,可以设置间隔距离,返回随机数
1 # 指定范围内随机,也可以说设置间隔距离,返回随机数
2 print(random.randrange(0,100))
3 # 执行结果
4 80
5
6 print(random.randrange(0,100,5))
7 # 执行结果
8 70
9
10 for i in range(3):
11 print(random.randrange(0,100,5))
12 # 执行结果
13 70
14 80
15 55
- (choice)在指定的列表中随机,返回随机数
1 # 在指定列表内随机,返回随机值
2 print(random.choice(["fs",2,"kz",90]))
3 # 执行结果
4 2
5
6 l = [10,23,63,123,634,12]
7 print(random.choice(l))
8 # 执行结果
9 634
10
11 for i in range(3):
12 print(random.choice(["fs",2,"kz",90]))
13 # 执行结果
14 90
15 2
16 90
- (shuffle)将指定列表进行打乱,返回None
1 # 指定列表进行打乱,返回None
2
3 l = [24,25,23,135,76,73,42321,57,23]
4 print(l)
5 # 执行结果
6 7.854645612968136
7
8 print(random.shuffle(l))
9 print(l)
10 # 执行结果
11 92.92847361436925
12 11.924828585708383
13 64.80197839949321
- (uniform)指定范围内随机,返回浮点型随机
1 # 指定范围内随机数,返回浮点数
2 print(random.uniform(1,100))
3 # 执行结果
4 7.854645612968136
5
6 for i in range(3):
7 print(random.uniform(1,100))
8 # 执行结果
9 92.92847361436925
10 11.924828585708383
11 64.80197839949321