math模块
# 数学相关模块import mathr = math.floor(3.2) # 向下取整print(r)r = math.ceil(4.5) # 向上取整print(r)r = round(3.4999) # 四舍五入print(r)r = math.pow(3,4) # =3**4print(r)r = math.sqrt(25) # 开平方print(r)r = math.fabs(-0.23) # 绝对值(小数)print(r)r = abs(-2) # 绝对值print(r)r = math.modf(3.14) # 拆分小数和整数print(r)r = math.copysign(3,-5) #-5的符号赋给3print(r)lt = [1,4,5,7] # 计算lt中数字的和(结果浮点数)r = math.fsum(lt)print(r)r = sum(lt) # 同上(结果整数)print(r)print(math.pi) # 圆周率print(math.e) # 自然对数# 随机数模块import randomr = random.random() # 随机获取0~1小数print(r)lt = [2,3,5,7,'A','C','d']r = random.choice(lt) # 随机获取lt中一个值print(r)random.shuffle(lt) #打乱列表print(lt)r = random.randrange(5,15,2) #获取制定范围内随机的整数print(r)r = random.uniform(4,24) #获取制定范围内随机数print(r)# 内建函数nums = [2,3,5,7,0,23,21,-11,0.4]print(max(nums)) # 获取最大值print(min(nums)) # 获取最小值# 进制相关hex(3)oct(3)bin(3)print(ord('a')) # 获取相对应的ascii码print(chr(90)) # 获取ascii码相对应的字符num = 99var = 'num + 1'nums = eval(var) # 将python字符串当做代码执行 慎用print(nums)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对武林站长站的支持。如果你想了解更多相关内容请查看下面相关链接
新闻热点
疑难解答