math模块

import math   # 先导入math包

1乘方&开方

import math

# 乘方开方,可以借助math中的pow函数
print(math.pow(10, 3))  # 10是底数,3是指数 1000.0
print(math.pow(27, 1 / 3))  # 3.0

  从上面的结果可以看到math.pow()函数得出的结果是浮点数。如果我们希望乘方的结果是整数的话,我们也可以使用下面的方法。

print(10**3)  # 1000

2上下取整

print(math.floor(3.14))  # 向下取整 3.0
print(math.ceil(3.14))  # 向上取整 4.0

3取最大最小值

print(min(1, 100, 90, 700))  # 1
print(max(1, 100, 90, 700))  # 700

4求和

print(sum([1, 2, 3, 4, 5]))  # 15

5同时取商和余数

print(divmod(10, 3))  # 求10除以3的商和余数 (3, 1)
原文地址:https://www.cnblogs.com/zhuzhaoli/p/14127150.html