import math
import random
#绝对值 abs
print(abs(-10))
#最大最小值 max min
print(max(1,2,3,7,5,4))
print(min(5,7,8,3,1,8))
# x的n次方 pow
print(pow(10,2))
print((pow(10,-2)))
#四舍五入 round
print(round(3.146))
print(round(3.546))
print(round(3.145,2))
#向上取整 math.ceil
print(math.ceil(18.1))
print(math.ceil(18.9))
#向下取整 math.floor
print(math.floor(18.1))
print(math.floor(18.9))
#返回一个数的整数和小数部分(返回的是一个元组) math.modf
print(math.modf(21.3))
#开方 math,sqrt
print(math.sqrt(16))
#随机数
#第一种:从序列的元素中随机一个元素
print(random.choice([1,2,3,4,5,6,7,8,9]))
#第二种:在(0,1,2,3,4)中随机一个数
print(random.choice(range(10)))
#第三种:在字符串种随机一个字符
print(random.choice("dashdghj"))
#产生1-100之间的数
print(random.choice(range(100))+1)
print(random.choice(range(1,101)))
#在一个范围内随机产生
print(random.randrange(1,100,2)) #产生一个随机奇数,2是步距(递增的间距),即:1,3,5,7,9。。。
#随机生成一个【0,1)的数
print(random.random()) #一个0到1之间的数,不包含1但包含0
#将序列的所有元素打乱,随机排序
l = [1,2,3,4,5,6]
random.shuffle(l)
print(l)
#随机生成一个范围内的实数
print(random.uniform(3,9))
#三角函数
print(math.sin(45))
print(math.cos(45))
运行结果:
10
7
1
100
0.01
3
4
3.15
19
19
18
18
(0.3000000000000007, 21.0)
4.0
4
9
j
5
11
73
0.2215736702858121
[1, 3, 6, 4, 5, 2]
3.700471056603738
0.8509035245341184
0.5253219888177297