1. 程式人生 > >python中關於函式的使用(對數函式,三角函式等)

python中關於函式的使用(對數函式,三角函式等)

 有錯的地方,或者不同意見的,煩請留言,或者發郵箱。

郵箱地址:[email protected]

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

python是用來匯入模組的,在python模組庫中有著大量的模組可供使用,要想使用這些檔案需要需要用import語句將指定模組匯入到當前程式中。所以使用函式,需要使用math模組。

[[email protected] 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