python中调用什么模块的什么函数来实现平方根_Python成为专业人士笔记–内置模块Modules和函数Functions...

“专业人士笔记”系列目录:创帆云:Python成为专业人士笔记--强烈建议收藏!每日持续更新!​zhuanlan.zhihu.com

模块是一个包含Python定义和语句的文件,而函数是执行逻辑的一段代码 。

要检查python中内置的函数,可以使用dir()。如果调用的时候不带任何参数,则返回当前范围中的名称。否则,返回一个按字母顺序排列的名称列表,其中包含(一些)给定对象的属性,以及从中可以访问的属性。

尝试运行如下命令显示所有函数:

dir(builtins)

输出:

[

'ArithmeticError',

'AssertionError',

'AttributeError',

'BaseException',

'BufferError',

'BytesWarning',

'DeprecationWarning',

'EOFError',

'Ellipsis',

'EnvironmentError',

'Exception',

'False',

'FloatingPointError',

'FutureWarning',

'GeneratorExit',

'IOError',

'ImportError',

'ImportWarning',

'IndentationError',

'IndexError',

'KeyError',

'KeyboardInterrupt',

'LookupError',

'MemoryError',

'NameError',

'None',

'NotImplemented',

'NotImplementedError',

'OSError',

'OverflowError',

'PendingDeprecationWarning',

'ReferenceError',

'RuntimeError',

'RuntimeWarning',

'StandardError',

'StopIteration',

'SyntaxError',

'SyntaxWarning',

'SystemError',

'SystemExit',

'TabError',

'True',

'TypeError',

'UnboundLocalError',

'UnicodeDecodeError',

'UnicodeEncodeError',

'UnicodeError',

'UnicodeTranslateError',

'UnicodeWarning',

'UserWarning',

'ValueError',

'Warning',

'ZeroDivisionError',

'debug',

'doc',

'import',

'name',

'package',

'abs',

'all',

'any',

'apply',

'basestring',

'bin',

'bool',

'buffer',

'bytearray',

'bytes',

'callable',

'chr',

'classmethod',

'cmp',

'coerce',

'compile',

'complex',

'copyright',

'credits',

'delattr',

'dict',

'dir',

'divmod',

'enumerate',

'eval',

'execfile',

'exit',

'file',

'filter',

'float',

'format',

'frozenset',

'getattr',

'globals',

'hasattr',

'hash',

'help',

'hex',

'id',

'input',

'int',

'intern',

'isinstance',

'issubclass',

'iter',

'len',

'license',

'list',

'locals',

'long',

'map',

'max',

'memoryview',

'min',

'next',

'object',

'oct',

'open',

'ord',

'pow',

'print',

'property',

'quit',

'range',

'raw_input',

'reduce',

'reload',

'repr',

'reversed',

'round',

'set',

'setattr',

'slice',

'sorted',

'staticmethod',

'str',

'sum',

'super',

'tuple',

'type',

'unichr',

'unicode',

'vars',

'xrange',

'zip'

]

要了解任何函数的功能及属性,我们可以使用内建函数帮助,方法是命令运行:help(函数名)

比如:

help(max)

输出:

Help on built-in function max in module builtin:

max(…)

max(iterable[, key=func]) -> value

max(a, b, c, …[, key=func]) -> value

在单个可迭代参数中,返回其最大的项。使用两个或多个参数,返回最大的参数。

而内建模块则包含一些额外的函数。例如,为了得到一个数字的平方根,我们需要包括数学(math)模块

import math

math.sqrt(16) # 输出4.0

为了了解模块中的所有函数,我们可以将函数名称分配给一个变量,然后打印该变量。

import math

print(dir(math))

输出:

['doc', 'name', 'package', 'acos', 'acosh',

'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign',

'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1',

'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma',

'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10',

'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt',

'tan', 'tanh', 'trunc']

除了函数之外,还可以在模块中提供文档。如果你有一个像这样的名为helloWorld.py的文件

""" 这是模块内函数的定义."""

def sayHello():

"""这是模块内函数的代码"""

return 'Hello World'

您可以像这样访问它:

import helloWorld

helloWorld.__doc__

'这是模块的描述'

helloWorld.sayHello.__doc__

'这是函数的描述'

对于所有用户定义的类型,都可以使用dir()来检索其属性、类的属性以及递归地检索其类的基类的属性

比如,创建一个class类:

class MyClassObject(object):

pass

我们来检索它的定义:

dir(MyClassObject)

输出:

['class', 'delattr', 'dict', 'doc', 'format', 'getattribute', 'hash',

'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr',

'sizeof', 'str', 'subclasshook', 'weakref']

任何数据类型都可以使用名为str的内置函数简单地转换为字符串。在将变量传递给print时,默认情况下都会调用该函数 ,比如:

str(123)

#输出 "123",因为默认的print打印都会转成Str字符串打印到屏幕

今天分享就到这里,禁止任何形式转载,违者必究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值