针对不同的数据类型,Python提供了一些内置函数,来完成一些常见的操作。
比如对于数字类型的变量,提供了绝对值函数、最大值函数等。
下表是常见的数字类型变量的常见操作。
函数 | 功能 | 示例 | 返回值 |
abs(x) | 返回x的绝对值 | abs(-2) | 2 |
math.ceil(x) | 返回x向上取整的整数 | math.ceil(5.23) | 6 |
math.floor(x) | 返回x向下取整的整数 | math.floor(5.23) | 5 |
math.exp(x) | 返回e的x次幂 | math.exp(2) | 7.389056 |
math.log(x) | 返回x的对数 | math.log(math.e) | 1 |
math.log(100,10) | 2 | ||
math.log10(x) | 返回以10为基数的x的对数 | math.log10(1000) | 3 |
math.log2(x) | 返回以2为基数的x的对数 | math.log2(16) | 4 |
max(x1,x2,…,xn) | 返回x1至xn的最大值 | max(1,2,3) | 3 |
min(x1,x2,…,xn) | 返回x1至xn的最小值 | min(1,2,3) | 1 |
pow(x,y) | 返回x的y次方 | pow(2,3) | 8 |
round(x[,n]) | 返回x的四舍五入 | round(2.234567,3) | 2.235 |
math.sqrt(x) | 返回x的平方根 | math.sqrt(16) | 4 |