python中关于函数的使用(对数函数,三角函数等)

 有错的地方,或者不同意见的,烦请留言,或者发邮箱。

邮箱地址:[email protected]

—————————————————————————————————

python是用来导入模块的,在python模块库中有着大量的模块可供使用,要想使用这些文件需要需要用import语句将指定模块导入到当前程序中。所以使用函数,需要使用math模块。

[gyz@archlinux job]$ python
Python 3.7.1 (default, Oct 22 2018, 10:41:28) 
[GCC 8.2.1 20180831] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import math

1,自然指数函数,即e^x

>>> math.exp(0)
1.0
>>> math.exp(1)
2.718281828459045

2,对数函数,即log(x)

在python中是log(x,a),即以a为底数,

当a=10时,

>>> math.log(1,10)
0.0
>>> math.log(10,10)
1.0
>>> math.log(100,10)
2.0

当a=e时,(在python中e=math.e)

>>> math.log(1,math.e)
0.0
>>> math.log(math.e,math.e)
1.0

3,正弦函数,即sin(x)

在python中,圆周率用math.pi表示

>>> math.sin(0)
0.0
>>> math.sin(math.pi/2)
1.0

4,余弦函数,即cos(x)

>>> math.cos(0)
1.0
>>> math.cos(math.pi)
-1.0

 5,绝对值,即|x|

>>> abs(10)
10
>>> abs(-10)
10
>>> 

6,求平方根

>>> math.sqrt(4)
2.0
>>> math.sqrt(9)
3.0

7,向上取整函数

>>> math.ceil(4.1)
5
>>> math.ceil(-4.1)
-4

 8,向下取整函数

>>> math.floor(4.1)
4
>>> math.floor(-4.1)
-5

9,取一组数中最小值的函数

>>> min(1,2,3)
1
>>> min(2,3,4)
2
>>> min(3,4,5)
3

10,取一组数中最大值的函数

>>> max(1,2,3)
3
>>> max(2,3,4)
4
>>> max(3,4,5)
5

 参考1:http://www.runoob.com/python/python-numbers.html

参考2:http://www.iplaypy.com/jinjie/import.html

猜你喜欢

转载自blog.csdn.net/weixin_39465823/article/details/85346017
今日推荐