Python数学函数

一、Python abs() 函数

返回上一级

Python 内置的 abs() 函数返回数字的绝对值

语法

abs( x )

参数

参数 说明
x 数值表达式,可以是整数,浮点数,复数

返回值

函数返回 x ( 数字 ) 的绝对值,如果参数是一个复数,则返回它的大小

例:

s=abs(-20)
x=abs(20.22)
y=abs(119)
print("abs(-20)=:",s)
print("abs(20.22)=:",x)
print("abs(119L)=:",y)
#运行结果
abs(-20)=: 20
abs(20.22)=: 20.22
abs(119L)=: 119

返回上一级

二、Python math.ceil() 函数

返回上一级

Python math.ceil(x) 函数返回一个大于或等于 x 的的最小整数, 俗称天花板取整,可以与[math.floor()](#六、Python math.floor() 函数)进行比较

导入模块

import math

语法

math.ceil( x )

参数

参数 说明
x 数值表达式

返回值

返回一个大于或等于 x 的的最小整数

例:

向上取整

import math
print("math.ceil(123.12)=:",math.ceil(123.12))
print("math.ceil(-20.22)=:",math.ceil(-20.22))
print("math.ceil(20.89)=:",math.ceil(20.89))
#运行结果
math.ceil(123.12)=: 124
math.ceil(-20.22)=: -20
math.ceil(20.89)=: 21

返回上一级

三、Python operator()函数

返回上一级

Python 3.X 的版本中已经没有 cmp 函数,如果你需要实现比较功能,需要引入 operator 模块,适合任何对象,包含的方法有:

语法

operator.lt(a, b)
operator.le(a, b)
operator.eq(a, b)
operator.ne(a, b)
operator.ge(a, b)
operator.gt(a, b)
operator.__lt__(a, b)
operator.__le__(a, b)
operator.__eq__(a, b)
operator.__ne__(a, b)
operator.__ge__(a, b)
operator.__gt__(a, b)
lt(a,b) 相当于 a<b #从第一个数字或字母(ASCII)比大小

le(a,b)相当于a<=b

eq(a,b)相当于a==b #字母完全一样,返回True,

ne(a,b)相当于a!=b

gt(a,b)相当于a>b

ge(a,b)相当于 a>=b
#函数的返回值是布尔哦

参数

参数 说明
x 数值表达式
y 数值表达式

返回值

  1. 如果 x < y 返回 -1
  2. 如果 x == y 返回 0
  3. 如果 x > y 返回 1

例:

import operator
print("operator.lt(2,3)=:",operator.lt(2,3))
print("operator.le(2,2)=:",operator.le(2,2))
print("operator.ge(3,2)=:",operator.ge(3,2))
print("operator.ge('python','Python')=:",operator.ge('python','Python'))
#运行结果
operator.lt(2,3)=: True
operator.le(2,2)=: True
operator.ge(3,2)=: True
operator.ge('python','Python')=: True

返回上一级

四、Python math.exp() 函数

返回上一级

Python math.exp() 函数返回 x 的指数 e^x^

导入模块

import math

语法

math.exp( x )

参数

参数 说明
x 数值表达式

返回值

x的指数 e^x^

例:

下面的代码使用 math.exp() 方法返回一些数值的指数

import math
print("math.exp(2)=:",math.exp(2))
print("math.exp(-45)=:",math.exp(-45))
#运行结果
math.exp(2)=: 7.38905609893065
math.exp(-45)=: 2.8625185805493937e-20

返回上一级

五、Python math.fabs() 函数

返回上一级

Python math.fabs() 函数返回数值的浮点数绝对值,如 math.fabs(-10) 返回 10.0

fabs() 函数类似于 [abs()](#一、Python abs() 函数) 函数,但是他有两点区别

  1. abs() 是内置函数。 fabs() 函数在 math 模块中定义
  2. fabs() 函数只对浮点型跟整型数值有效, abs() 还可以运用在复数中

导入模块

import math

语法

math.fabs( x )

参数

参数 返回值
x 数值表达式

返回值

数字的浮点数形式的绝对值

例:

下面的代码使用 math.fabs() 返回一些数值的浮点类型绝对值

import math
print("math.fabs(math.pi)=:",math.fabs(math.pi))
print("math.fabs(-20.22)=:",math.fabs(-20.22))
print("math.fabs(22.20)=:",math.fabs(22.20))
#运行结果
math.fabs(math.pi)=: 3.141592653589793
math.fabs(-20.22)=: 20.22
math.fabs(22.20)=: 22.2

返回上一级

六、Python math.floor() 函数

返回上一级

Python math.floor() 函数返回数字的下舍整数,俗称地板流取整

可以与[math.ceil()](#二、Python math.ceil() 函数)进行比较。

导入模块

import math

语法

math.floor( x )

参数

参数 说明
x 数值表达式

返回值

数字的下舍整数

例:

下面的代码使用 math.floor() 函数给一些数值向下取整

import math					#向下取整
print("math.floor(-20.22)=:",math.floor(-20.22))
print("math.floor(20.04)=:",math.floor(20.04))
#运行结果
math.floor(-20.22)=: -21
math.floor(20.04)=: 20

返回上一级

七、Python math.log() 函数

返回上一级

Python math.log() 函数返回 x 的自然对数

导入模块

import math

语法

math.log( x )

参数

参数 说明
x 数值表达式

返回值

返回 x 的自然对数,x > 0

例:

下面的代码使用 math.log() 函数返回一些数值的自然对数

import math					#对自然数取对数
print("math.log(10)=:",math.log(10))
print("math.log(4)=:",math.log(4))
print("math.log(2022)=:",math.log(2022))
#运行结果
math.log(10)=: 2.302585092994046
math.log(4)=: 1.3862943611198906
math.log(2022)=: 7.611842399580417

返回上一级

八、Python math.log10() 函数

返回上一级

Python math.log10() 函数返回返回以 10 为基数的 x 对数,即 log10x

导入模块

import math

语法

math.log10( x )

参数

参数 说明
x 数值表达式

返回值

返回以 10 为基数的 x 对数,x>0

例:

下面的代码使用 math.log10() 求一些数值的以 10 为底的对数值

import math					#以10为底取对数
print("math.log10(11.11)=:",math.log10(11.11))
print("math.log10(2022)=:",math.log10(2022))
print("math.log10(10)=:",math.log10(10))
#运行结果
math.log10(11.11)=: 1.0457140589408676
math.log10(2022)=: 3.3057811512549824
math.log10(10)=: 1.0

返回上一级

九、Python max() 函数

返回上一级

Python 内置的 max() 方法返回给定参数的最大值,参数可以为序列

max() 可应用于 列表、元组、数字、字符串。

max() 语法

max( x, y, z, .... )

参数

参数 说明
x 数值表达式
y 数值表达式
z 数值表达式
更多数值表达式

返回值

给定参数的最大值

例:

下面的代码使用 max() 函数求取一些数值中的最大值

print("max(12,3.2,34.2,12.3)=:",max(12,3.2,34.2,12.3))			#求最大值
print ( "max(80, 100, 1000)=: ", max(80, 100, 1000) )
print ( "max(-20, 100, 400)=: ", max(-20, 100, 400) )
print ( "max(-80, -20, -10)=: ", max(-80, -20, -10) )
print ( "max(0, 100, -400)=: ", max(0, 100, -400) )
#运行结果
max(12,3.2,34.2,12.3)=: 34.2
max(80, 100, 1000)=: 1000
max(-20, 100, 400)=: 400
max(-80, -20, -10)=: -10
max(0, 100, -400)=: 100

返回上一级

十、Python min() 函数

返回上一级

Python 内置的 min() 方法返回给定参数的最小值,参数可以为序列

min() 可用于 列表、元组、数字、字符串。

min() 语法

min( x, y, z, .... )

参数

参数 说明
x 数值表达式
y 数值表达式
z 数值表达式
更多数值表达式

返回值

给定参数的最小值

例:

下面的代码使用 max() 函数求取一些数值中的最小值

print("min(12,3.2,34.2,12.3)=:",min(12,3.2,34.2,12.3))	#求最小值
print ( "min(80, 100, 1000)=: ", min(80, 100, 1000) )
print ( "min(-20, 100, 400)=: ", min(-20, 100, 400) )
print ( "min(-80, -20, -10)=: ", min(-80, -20, -10) )
print ( "min(0, 100, -400)=: ", min(0, 100, -400) )
#运行结果
min(12,3.2,34.2,12.3)=: 3.2
min(80, 100, 1000)=: 80
min(-20, 100, 400)=: -20
min(-80, -20, -10)=: -80
min(0, 100, -400)=: -400

返回上一级

十一、Python math.modf() 函数

返回上一级

Python math.modf() 函数返回 x 的小数部分与整数部分组成的二元组,两部分的数值符号与 x 相同

整数部分以浮点型表示

导入模块

import math

语法

math.modf( x )

参数

参数 说明
x 数值表达式

返回值

返回 x 的整数部分与小数部分

例:

下面的代码使用 math.modf() 返回一些数值的整数部分和小数部分

import math

print("math.domf(100.12)",math.modf(100.12))
print("math.modf(100.17)=:",math.modf(100.17))
print("math.modf(-math.pi)=:",math.modf(-math.pi))
#运行结果
math.domf(100.12) (0.12000000000000455, 100.0)
math.modf(100.17)=: (0.1700000000000017, 100.0)
math.modf(-math.pi)=: (-0.14159265358979312, -3.0)

返回上一级

十二、Python math.pow() 函数

返回上一级

Python math.pow() 方法返回 xy ( x 的 y 次方 ) 的值

导入模块

import math

语法

math.pow( x, y [,z])

函数是计算 x 的 y 次方,如果 z 在存在,则再对结果进行取模,其结果等效于 pow(x,y) %z。

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

注意:

内置的 **pow()**会把参数作为整型

而 math.pow() 方法则会把参数转换为 float

参数

参数 说明
x 数值表达式
y 数值表达式
z 数值表达式

返回值

返回 xy ( x 的 y 次方 ) 的值

例:

import math   # 导入 math 模块

print ( "math.pow(100, 2) : ", math.pow(100, 2))
# 使用内置,查看输出结果区别
print ( "pow(100, 2) : ", pow(100, 2))
print("pow(10,2,2) :",pow(10,2,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))
#运行结果
math.pow(100, 2) : 10000.0
pow(100, 2) : 10000
pow(10,2,2) : 0
math.pow(100, -2) : 0.0001
math.pow(2, 4) : 16.0
math.pow(3, 0) : 1.0

返回上一级

十三、Python round() 函数

返回上一级

Python 内建的 round() 函数用于返回浮点数 x 的四舍五入值

语法

round( x [, n]  )

如果只写round(x)默认保留整数。

参数

参数 说明
x 数值表达式
n 数值表达式

返回值

返回浮点数 x 的四舍五入值

例:

下面的代码列出了一些浮点数的四舍五入值

import math
print("round(math.pi,2)=:",round(math.pi,2))
print("round(2.022,2)=:",round(2.022,2))
print("round(20.22)=:",round(20.22))
#运行结果
round(math.pi,2)=: 3.14
round(2.022,2)=: 2.02
round(20.22)=: 20

返回上一级

十四、Python math.sqrt() 函数

返回上一级

Python math.sqrt() 函数返回数字 x 的平方根

导入模块

import math

语法

math.sqrt( x )

参数

参数 说明
x 数值表达式

返回值

数字 x 的平方根

说明

下面的代码使用 math.sqrt() 函数返回一些数值的平方根

import math             #平方根
print("math.sqrt(20)=:",math.sqrt(20))
print("math.sqrt(4)=:",math.sqrt(4))
print("math.sqrt(20.22)=:",math.sqrt(20.22))
#运行结果
math.sqrt(20)=: 4.47213595499958
math.sqrt(4)=: 2.0
math.sqrt(20.22)=: 4.496665431183423

返回上一级