Python-基础-数字函数

绝对值函数

abs(x) x – 数值表达式,可以是整数,浮点数,复数。
fabs(x) x – 数值表达式,可以是整数,浮点数。

区别:
abs() 是内置函数。 fabs() 函数在 math 模块中定义。
fabs() 函数只对浮点型跟整型数值有效。 abs() 还可以运用在复数中。
fabs()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。

print ("abs(10) : ", abs(10))
print ("abs(-40) : ", abs(-40))
print ("abs(100.10) : ", abs(100.10))
print ("abs(-10.23) : ", abs(-10.23))
print ("abs(1+2j) : ", abs(1+2j))

print ("math.fabs(10) : ", math.fabs(10))
print ("math.fabs(-40) : ", math.fabs(-40))
print ("math.fabs(100.10) : ", math.fabs(100.10))
print ("math.fabs(-10.23) : ", math.fabs(-10.23))

取整函数

ceil(x) 向上取整 返回一个大于或等于 x 的的最小整数。
floor(x) 向下取整 返回一个小或等于 x 的的最小整数。
x – 数值表达式,可以是整数,浮点数。

print ("math.ceil(-1.25) : ", math.ceil(-1.25))
print ("math.ceil(10.465) : ", math.ceil(10.465))
print ("math.ceil(10.89) : ", math.ceil(10.89))
print ("math.ceil(math.pi) : ", math.ceil(math.pi))

print ("math.floor(-1.25) : ", math.floor(-1.25))
print ("math.floor(10.465) : ", math.floor(10.465))
print ("math.floor(10.89) : ", math.floor(10.89))
print ("math.floor(math.pi) : ", math.floor(math.pi))

最值函数

max(x1, x2,…) 返回给定参数的最大值,参数可以为序列。
min(x1, x2,…) 返回给定参数的最小值,参数可以为序列。

print ("max(1, 2, 3) : ", max(1, 2, 3))
print ("max(-1, -2, -3) : ", max(-1, -2, -3))
print ("max(-1, 2, 0) : ", max(-1, 2, 0))
print ("max(0.5, -3.5, -10) : ", max(0.5, -3.5, -10))
print ("min(1, 2, 3) : ", min(1, 2, 3))
print ("min(-1, -2, -3) : ", min(-1, -2, -3))
print ("min(-1, 2, 0) : ", min(-1, 2, 0))
print ("min(0.5, -3.5, -10) : ", min(0.5, -3.5, -10))

幂函数,指数函数,对数函数

exp(x) 返回e的x次幂(ex)
log(x) 返回x的自然对数,x > 0
log10(x) 返回以10为基数的x对数,x>0
modf(x) 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。
sqrt(x) 返回数字x的平方根。

print ("math.exp(-5.17) : ", math.exp(-5.17))
print ("math.exp(55) : ", math.exp(55))
print ("math.exp(10.12) : ", math.exp(10.12))
print ("math.exp(3.72) : ", math.exp(3.72))
print ("math.exp(math.pi) : ", math.exp(math.pi))

print ("math.log(55) : ", math.log(55))
print ("math.log(10.12) : ", math.log(10.12))
print ("math.log(3.72) : ", math.log(3.72))
print ("math.log(math.pi) : ", math.log(math.pi))

print ("math.log10(55) : ", math.log10(55))
print ("math.log10(10.12) : ", math.log10(10.12))
print ("math.log10(3.72) : ", math.log10(35.72))
print ("math.log10(math.pi) : ", math.log10(math.pi))

print ("math.modf(-5.17) : ", math.modf(-5.17))
print ("math.modf(55) : ", math.modf(55))
print ("math.modf(10.12) : ", math.modf(10.12))
print ("math.modf(3.72) : ", math.modf(3.72))
print ("math.modf(math.pi) : ", math.modf(math.pi))

print ("math.sqrt(-5.17) : ", math.sqrt(25))
print ("math.sqrt(55) : ", math.sqrt(55))
print ("math.sqrt(10.12) : ", math.sqrt(10.12))
print ("math.sqrt(3.72) : ", math.sqrt(7))

pow(x, y) x**y 运算后的值。
注意:pow() 通过内置的方法直接调用,内置方法会把参数作为整型,而 math 模块则会把参数转换为 float。

print ("math.pow(100, 2) : ", math.pow(100, 2))
# 使用内置,查看输出结果区别
print ("pow(100, 2) : ", pow(100, 2))
print ("math.pow(100, -2) : ", math.pow(100, -2))
print ("math.pow(2, 4) : ", math.pow(2, 4))
print ("math.pow(3, 0) : ", math.pow(3, 0))

四舍五入

round(x [,n]) 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。
x – 数字表达式。
n – 表示从小数点位数,其中 x 需要四舍五入,默认值为 0。

当个位为奇数,小数部分>=0.5入,其余为舍
当个位为偶数,小数部分>0.5入,其余为舍。

print ("round(10.40) : ", round(10.40))
print ("round(10.50) : ", round(10.50))
print ("round(10.60) : ", round(10.60))
print ("round(10.70) : ", round(10.70))

print ("round(11.40) : ", round(11.40))
print ("round(11.50) : ", round(11.50))
print ("round(11.60) : ", round(11.60))
print ("round(11.70) : ", round(11.70))

随机数函数

choice(seq) 从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。
random() 随机生成下一个实数,它在[0,1)范围内。
seed([x]) 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。
shuffle(lst) 将序列的所有元素随机排序
uniform(x, y) 随机生成下一个实数,它在[x,y]范围内。

print ("从 range(100) 返回一个随机数 : ",random.choice(range(100)))
print ("从列表中 [1, 2, 3, 4, 5, 6, 7, 8, 9]) 返回一个随机元素 : ", random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9]))
print ("从字符串中 'abcdefg' 返回一个随机字符 : ", random.choice('abcdefg'))

#随机数
print ("random() : ", random.random())
random.seed()
print ("使用默认种子生成随机数:", random.random())

random.seed(10)
print ("使用整数种子生成随机数:", random.random())

random.seed("hello",2)
print ("使用字符串种子生成随机数:", random.random())

ls = [1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(ls)
print ("随机排序列表 : ",  ls)
print ("随机生成10,100.5之间的实数 : ",  random.uniform(10,100.5))

randrange ([start,] stop [,step]) 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1
start – 指定范围内的开始值,包含在范围内。
stop – 指定范围内的结束值,不包含在范围内。
step – 指定递增基数。

# 从 1-100 中选取一个奇数
print ("randrange(1,100, 2) : ", random.randrange(1, 100, 2))

# 从 0-99 选取一个随机数
print ("randrange(100) : ", random.randrange(100))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值