当我们使用 Python 进行各种分析时,会用到各种函数。比如我们在使用SQL的时候,经常会用到join、max等各种函数。如果我们想看看Python有没有这个功能,这个时候可能是可以的。大多数人都知道百度,那么如何使用Python本身来查找函数并学习如何使用它们而不是百度呢?下面,小白总结了一些自己的经历~
比如我们在使用math模块,但是不知道这个模块下有没有我们自己常用的函数,那怎么办呢?
方法一
import math
dir(math)
首先我们导入这个模块,使用dir函数查看这个模块下有哪些函数。
['__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'acos',
'acosh',
'asin',
'asinh',
'atan',
'atan2',
'atanh',
'ceil',
'copysign',
'cos',
'cosh',
'degrees',
'e',
'erf',
'erfc',
'exp',
'expm1',
'fabs',
'factorial',
'floor',
'fmod',
'frexp',
'fsum',
'gamma',
'gcd',
'hypot',
'inf',
'isclose',
'isfinite',
'isinf',
'isnan',
'ldexp',
'lgamma',
'log',
'log10',
'log1p',
'log2',
'modf',
'nan',
'pi',
'pow',
'radians',
'sin',
'sinh',
'sqrt',
'tan',
'tanh',
'tau',
'trunc']
这个方法是获取函数列表,当然这里也可以使用帮助函数:
import math
help(math)
如果对函数还不是很了解,可以到方法文件中查看函数的定义,使用***.__file__查看位置,然后打开后缀为.py的文件。
import random
random.__file__
结果为:这样就可以到这个py文件中查看源码
'D:\\Anaconda2\\envs\\py3\\lib\\random.py'
这里需要注意一下:
***.pyc文件是编译后的文件,打开看不懂,所以看***.py文件。
在里面,你可以搜索你想看的功能,以及具体的定义。例如,你搜索 expovariate 函数,并粘贴下面的方法,以便你可以看到该方法是如何声明的。方便且易于理解吗?更清楚了~
def expovariate(self, lambd):
"""Exponential distribution.
lambd is 1.0 divided by the desired mean. It should be
nonzero. (The parameter would be called "lambda", but that is
a reserved word in Python.) Returned values range from 0 to
positive infinity if lambd is positive, and from negative
infinity to 0 if lambd is negative.
"""
# lambd: rate lambd = 1/mean
# ('lambda' is a Python reserved word)
# we use 1-random() instead of random() to preclude the
# possibility of taking the log of zero.
return -_log(1.0 - self.random())/lambd
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何使用 python 随机模块中的 randint() 函数?08/18
- ♥ Python判断端口是否被占用11/12
- ♥ mysql的2005错误是什么意思?09/12
- ♥ python安装完成无法使用怎么解决10/15
- ♥ 如何在python中打开命令行09/26
- ♥ 如何使用python定期获取网页内容10/21
内容反馈