欢迎访问宙启技术站
智能推送

标题:Python中的数学函数集锦

发布时间:2023-06-25 05:26:09

Python作为一种高级编程语言,提供了许多数学函数,使数学计算变得更加简单和高效。这些数学函数可以帮助程序员进行各种计算,从简单的算术运算到复杂的统计学和科学计算。

本文将介绍Python中的一些常见数学函数,包括基本数学函数、科学计算函数和统计函数。这些函数可以在Python内置的数学库中找到,我们将详细讨论这些函数的语法、用法和例子。

基本数学函数

1. abs()

函数abs()返回给定数字的绝对值。如果数字是复数,则返回其模长。

语法:abs( x )

参数:x(数字)

返回值:x的绝对值

例:

print(abs(-6))   # 6
print(abs(-5.5)) # 5.5
print(abs(5+2j)) # 5.385164807134504

2. pow()

函数pow()返回给定数字的幂。参数base是底数,exponent是指数。

语法:pow( base, exponent )

参数:base(数字),exponent(数字)

返回值:base的exponent次幂

例:

print(pow(2, 3))   # 8
print(pow(2, -3))  # 0.125
print(pow(1.5, 2)) # 2.25

3. round()

函数round()返回给定数字的四舍五入值。参数x是要舍入的数字。precision是舍入后要保留的小数位数,默认为0。

语法:round( x, precision )

参数:x(数字),precision(数字)

返回值:x舍入后的值

例:

print(round(2.5678))   # 3
print(round(2.5678, 2)) # 2.57
print(round(-2.5678, 2))# -2.57

4. max()

函数max()返回给定参数中的最大值。

语法:max( arg1, arg2, ... )

参数:arg1, arg2, ...(数字)

返回值:最大值

例:

print(max(2, 3, 5, 1)) # 5
print(max(3.14, 2.8, 1.5)) #3.14

5. min()

函数min()返回给定参数中的最小值。

语法:min( arg1, arg2, ... )

参数:arg1, arg2, ...(数字)

返回值:最小值

例:

print(min(2, 3, 5, 1)) # 1
print(min(3.14, 2.8, 1.5)) #1.5

科学计算函数

6. sqrt()

函数sqrt()返回给定数字的平方根。

语法:sqrt( x )

参数:x(数字)

返回值:x的平方根

例:

import math
print(math.sqrt(25)) # 5.0
print(math.sqrt(2)) # 1.4142135623730951

7. exp()

函数exp()返回给定数字的指数文件。

语法:exp( x )

参数:x(数字)

返回值:e的x次方

例:

import math
print(math.exp(1)) # 2.718281828459045
print(math.exp(2)) # 7.3890560989306495

8. log()

函数log()返回给定数字的自然对数或对数。

语法:log( x, base )

参数:x(数字),base(数字)

返回值:x的对数

例:

import math
print(math.log(10)) #2.302585092994046
print(math.log(100, 10)) #2.0

9. sin(), cos(), tan()

函数sin()、cos()、tan()分别返回给定角度(以弧度为单位)的正弦、余弦和正切值。

语法:sin( x ), cos( x ), tan( x )

参数:x(弧度)

返回值:x的正弦、余弦和正切值

例:

import math
print(math.sin(0.5))  # 0.479425538604203
print(math.cos(0.5))  # 0.8775825618903728
print(math.tan(0.5))  # 0.5463024898437905

10. asin(), acos(), atan()

函数asin()、acos()、atan()分别返回给定正弦、余弦和正切值的角度(以弧度为单位)。

语法:asin( x ), acos( x ), atan( x )

参数:x(弧度)

返回值:x的角度

例:

import math
print(math.asin(0.5))  # 0.5235987755982988
print(math.acos(0.5))  # 1.0471975511965979
print(math.atan(0.5))  # 0.4636476090008061

统计函数

11. sum()

函数sum()返回给定列表中所有数字的总和。

语法:sum( iterable )

参数:iterable(列表、元组、集合等)

返回值:所有数字的总和

例:

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

12. mean()

函数mean()返回给定列表中所有数字的平均值。

语法:mean( iterable )

参数:iterable(列表、元组、集合等)

返回值:所有数字的平均值

例:

import statistics
print(statistics.mean([1, 2, 3, 4, 5])) # 3
print(statistics.mean((1, 2, 3, 4, 5))) # 3
print(statistics.mean({1, 2, 3, 4, 5})) # 3

13. median()

函数median()返回给定列表中所有数字的中位数。

语法:median( iterable )

参数:iterable(列表、元组、集合等)

返回值:所有数字的中位数

例:

import statistics
print(statistics.median([1, 2, 3, 4, 5])) # 3
print(statistics.median((1, 2, 3, 4, 5))) # 3
print(statistics.median({1, 2, 3, 4, 5})) # 3

14. mode()

函数mode()返回给定列表中出现次数最多的数字。

语法:mode( iterable )

参数:iterable(列表、元组、集合等)

返回值:出现次数最多的数字

例:

import statistics
print(statistics.mode([1, 2, 3, 3, 4, 5, 5, 5])) # 5
print(statistics.mode((1, 2, 3, 3, 4, 5, 5, 5))) # 5
print(statistics.mode({1, 2, 3, 3, 4, 5, 5, 5})) # 5

结论

多数数学函数在 Python 的内置 math 模块中都有定义。当你需要像三角函数、对数、指数乘方等进行计算时,就可以使用这些函数,这样就不必自己编写算法进行计算。此外,Python 还提供了其他的模块,比如 statistics 模块用于统计计算。学会使用 Python 内置的数学函数,能够提高计算效率,进而提高程序编写效率。