python-数字
- 数学函数
abs( x ),x为数值表达式,可以是整数,浮点数,复数,
返回 x(数字)的绝对值,如果参数是一个复数,则返回它的大小(模)
print ("abs(-40) : ", abs(-40))
# abs(-40) : 40
print ("abs(100.10) : ", abs(100.10))
# abs(100.10) : 100.1
math.ceil( x ),x为数值表达式
函数返回返回一个大于或等于 x 的的最小整数
import math # 导入 math 模块
print ("math.ceil(-45.17) : ", math.ceil(-45.17))
# math.ceil(-45.17) : -45
print ("math.ceil(100.12) : ", math.ceil(100.12))
# math.ceil(100.12) : 101
print ("math.ceil(100.72) : ", math.ceil(100.72))
# math.ceil(100.72) : 101
print ("math.ceil(math.pi) : ", math.ceil(math.pi))
# math.ceil(math.pi) : 4
math.exp( x ),x为数值表达式
返回x的指数,ex
import math # 导入 math 模块
print ("math.exp(-45.17) : ", math.exp(-45.17))
# math.exp(-45.17) : 2.4150062132629406e-20
print ("math.exp(100.12) : ", math.exp(100.12))
# math.exp(100.12) : 3.0308436140742566e+43
print ("math.exp(100.72) : ", math.exp(100.72))
# math.exp(100.72) : 5.522557130248187e+43
print ("math.exp(math.pi) : ", math.exp(math.pi))
# math.exp(math.pi) : 23.140692632779267
math.fabs( x ),x为数值表达式
返回数字的绝对值
abs() 是内置函数。 fabs() 函数在 math 模块中定义
fabs() 函数只对浮点型跟整型数值有效。 abs() 还可以运用在复数中
import math # 导入 math 模块
print ("math.fabs(-45.17) : ", math.fabs(-45.17))
# math.fabs(-45.17) : 45.17
print ("math.fabs(100.12) : ", math.fabs(100.12))
# math.fabs(100.12) : 100.12
print ("math.fabs(100.72) : ", math.fabs(100.72))
# math.fabs(100.72) : 100.72
print ("math.fabs(math.pi) : ", math.fabs(math.pi))
# math.fabs(math.pi) : 3.141592653589793
math.floor( x ),x为数值表达式
返回小于或等于 x 的整数
import math # 导入 math 模块
print ("math.floor(-45.17) : ", math.floor(-45.17))
# math.floor(-45.17) : -46
print ("math.floor(100.12) : ", math.floor(100.12))
# math.floor(100.12) : 100
print ("math.floor(100.72) : ", math.floor(100.72))
# math.floor(100.72) : 100
print ("math.floor(math.pi) : ", math.floor(math.pi))
# math.floor(math.pi) : 3
math.log( x ),x为数值表达式
返回x的自然对数,x>0
import math # 导入 math 模块
print ("math.log(100.12) : ", math.log(100.12))
# math.log(100.12) : 4.6063694665635735
print ("math.log(100.72) : ", math.log(100.72))
# math.log(100.72) : 4.612344389736092
print ("math.log(math.pi) : ", math.log(math.pi))
# math.log(math.pi) : 1.1447298858494002
math.log10( x ),x为数值表达式
返回以10为基数的x对数,x>0
import math # 导入 math 模块
print ("math.log10(100.12) : ", math.log10(100.12))
# math.log10(100.12) : 2.0005208409361854
print ("math.log10(100.72) : ", math.log10(100.72))
# math.log10(100.72) : 2.003115717099806
print ("math.log10(119) : ", math.log10(119))
# math.log10(119) : 2.075546961392531
print ("math.log10(math.pi) : ", math.log10(math.pi))
# math.log10(math.pi) : 0.4971498726941338
max( x, y, z, .... ),x,y,z均为数值表达式
返回给定参数的最大值
同时也有min来求解最小值
print ("max(80, 100, 1000) : ", max(80, 100, 1000))
# max(80, 100, 1000) : 1000
print ("max(-20, 100, 400) : ", max(-20, 100, 400))
# max(-20, 100, 400) : 400
print ("max(-80, -20, -10) : ", max(-80, -20, -10))
# max(-80, -20, -10) : -10
print ("max(0, 100, -400) : ", max(0, 100, -400))
# max(0, 100, -400) : 100
math.modf( x ),x均为数值表达式
返回x的整数部分与小数部分,为一个元组(小数,整数),两者均是浮点数
import math # 导入 math 模块
print ("math.modf(100.12) : ", math.modf(100.12))
# math.modf(100.12) : (0.12000000000000455, 100.0)
print ("math.modf(100.72) : ", math.modf(100.72))
# math.modf(100.72) : (0.7199999999999989, 100.0)
print ("math.modf(119) : ", math.modf(119))
# math.modf(119) : (0.0, 119.0)
print ("math.modf(math.pi) : ", math.modf(math.pi))
# math.modf(math.pi) : (0.14159265358979312, 3.0)
math.pow( x, y ),x,y均为数值表达式
返回 xy(x的y次方) 的值
内置函数pow(x,y)也可以实现同样的效果
但是math模块的结果为浮点数,而内置函数的结果为整型
import math # 导入 math 模块
print ("math.pow(100, 2) : ", math.pow(100, 2))
# math.pow(100, 2) : 10000.0
# 使用内置,查看输出结果区别
print ("pow(100, 2) : ", pow(100, 2))
# pow(100, 2) : 10000
print ("math.pow(100, -2) : ", math.pow(100, -2))
# math.pow(100, -2) : 0.0001
print ("math.pow(2, 4) : ", math.pow(2, 4))
# math.pow(2, 4) : 16.0
print ("math.pow(3, 0) : ", math.pow(3, 0))
# math.pow(3, 0) : 1.0
round( x [, n] ),x为数字表达式,n表示小数点后位数,默认为0
返回浮点数x的四舍五入值
注意, 遇到 x.5的数字,如果x是偶数取得是x,如果x是奇数,取得的是x+1
如果想要实现精确的四舍五入,参见decimal模块
print ("round(70.23456) : ", round(70.23456))
# round(70.23456) : 70
print ("round(56.659,1) : ", round(56.659,1))
# round(56.659,1) : 56.7
print ("round(80.264, 2) : ", round(80.264, 2))
# round(80.264, 2) : 80.26
print ("round(100.000056, 3) : ", round(100.000056, 3))
# round(100.000056, 3) : 100.0
print ("round(-100.000056, 3) : ", round(-100.000056, 3))
# round(-100.000056, 3) : -100.0
math.sqrt( x ),x为数字表达式
返回数字x的平方根,始终为浮点类型
import math # 导入 math 模块
print ("math.sqrt(100) : ", math.sqrt(100))
# math.sqrt(100) : 10.0
print ("math.sqrt(7) : ", math.sqrt(7))
# math.sqrt(7) : 2.6457513110645907
print ("math.sqrt(math.pi) : ", math.sqrt(math.pi))
# math.sqrt(math.pi) : 1.7724538509055159
- 随机数函数
random.choice( seq ),seq可以是一个列表,元组或字符串
返回随机项
import random
print ("从 range(100) 返回一个随机数 : ",random.choice(range(100)))
# 从 range(100) 返回一个随机数 : 68
print ("从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 : ", random.choice([1, 2, 3, 5, 9]))
# 从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 : 2
print ("从字符串中 'Runoob' 返回一个随机字符 : ", random.choice('Runoob'))
# 从字符串中 'Runoob' 返回一个随机字符 : u
random.randrange ([start,] stop [,step]),start为指定范围内的开始值,包含在范围内,stop为指定范围内的结束值,不包含在范围内,step为指定递增基数
从给定的范围返回随机项
import random
# 从 1-100 中选取一个奇数
print ("randrange(1,100, 2) : ", random.randrange(1, 100, 2))
# randrange(1,100, 2) : 97
# 从 0-99 选取一个随机数
print ("randrange(100) : ", random.randrange(100))
# randrange(100) : 42
random.random(),无参数
返回随机生成的一个实数,它在[0,1)范围内
可以通过乘法实现任意范围
import random
# 第一个随机数
print ("random() : ", random.random())
# random() : 0.09690599908884856
# 实现 100-120 随机小数(1)
print((random.random()*21)+100)
# 实现 100-101 随机小数(2)
print(random.uniform(100, 101))
random.seed ( [x] ),x为改变随机数生成器的种子seed。不必特别去设定seed,Python会帮你选择seed
无返回值
指定了随机种子,每次随机的结果都是相同的
如果不去指定种子,系统会自动更换种子,所以每一次随机的结果都是不同的
import random
random.seed()
print ("使用默认种子生成随机数:", random.random())
# 使用默认种子生成随机数: 0.7908102856355441
print ("使用默认种子生成随机数:", random.random())
# 使用默认种子生成随机数: 0.81038961519195
random.seed(10)
print ("使用整数 10 种子生成随机数:", random.random())
# 使用整数 10 种子生成随机数: 0.5714025946899135
random.seed(10)
print ("使用整数 10 种子生成随机数:", random.random())
# 使用整数 10 种子生成随机数: 0.5714025946899135
random.seed("hello",2)
print ("使用字符串种子生成随机数:", random.random())
# 使用字符串种子生成随机数: 0.3537754404730722
random.shuffle (lst ),lst为列表
返回None
import random
list = [20, 16, 10, 5];
random.shuffle(list)
print ("随机排序列表 : ", list)
# 随机排序列表 : [20, 5, 16, 10]
random.shuffle(list)
print ("随机排序列表 : ", list)
# 随机排序列表 : [5, 20, 10, 16]
random.uniform(x, y),x 为随机数的最小值,y为随机数的最大值
返回一个浮点数 N,取值范围为如果 x<y 则 x <= N <= y,如果 y<x 则y <= N <= x
也就是生成闭区间的随机数
import random
print ("uniform(5, 10) 的随机浮点数 : ", random.uniform(5, 10))
# uniform(5, 10) 的随机浮点数 : 7.054602800254241
print ("uniform(7, 14) 的随机浮点数 : ", random.uniform(7, 14))
# uniform(7, 14) 的随机浮点数 : 12.552229882744296
random.randint(x,y)
随机生一个整数int类型,可以指定这个整数的范围
random.randint(1000,9999)
# 8449
random.sample(sequence,length) 可以从指定的序列中,随机的截取指定长度的片断,不修改原序列
lst = random.sample('abcd1234',4)
strs = ''.join(lst)
strs
# 'a432'
- 三角函数
略
- 数学常量
圆周率pi和自然常数e
- ASCII码和字符转换
ord()用于将字符转换为ASCII码
chr()用于将ASCII码转换为字符(为str类型)

浙公网安备 33010602011771号