Python,就是新手最大的敌人

本文介绍了如何通过import导入模块后使用dir()函数列出所有函数,包括math和__builtins__模块的实例,并展示了查看函数文档的方法。重点讲解了查看内置函数清单和利用help()获取帮助信息的操作。
摘要由CSDN通过智能技术生成

列出模块中的函数

用import导入模块后,可使用函数dir(m)列出模块的所有函数,import是导入模块的命令,m是模块名。

例子:

1

2

3

>>>import math    

>>>dir(math)

['__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']

这个例子列出math模块的一些函数,以双下划线( _ _ )开头的名称用于较复杂的python编程。

2.查看完整的python内置函数清单

查看完整的python内置函数清单,可在提示符后输入 >>>dir(_ _builtins_ _)。

例子:

1

2

3

>>> dir(__builtins__)

['ArithmeticError''AssertionError''AttributeError''BaseException''BlockingIOError''BrokenPipeError''BufferError''BytesWarning''ChildProcessError''ConnectionAbortedError''ConnectionError''ConnectionRefusedError''ConnectionResetError''DeprecationWarning''EOFError''Ellipsis''EnvironmentError''Exception''False''FileExistsError''FileNotFoundError''FloatingPointError''FutureWarning''GeneratorExit''IOError''ImportError''ImportWarning''IndentationError''IndexError''InterruptedError''IsADirectoryError''KeyError''KeyboardInterrupt''LookupError''MemoryError''ModuleNotFoundError''NameError''None''NotADirectoryError''NotImplemented''NotImplementedError''OSError''OverflowError''PendingDeprecationWarning''PermissionError''ProcessLookupError''RecursionError''ReferenceError''ResourceWarning''RuntimeError''RuntimeWarning''StopAsyncIteration''StopIteration''SyntaxError''SyntaxWarning''SystemError''SystemExit''TabError''TimeoutError''True''TypeError''UnboundLocalError''UnicodeDecodeError''UnicodeEncodeError''UnicodeError''UnicodeTranslateError''UnicodeWarning''UserWarning''ValueError''Warning''WindowsError''ZeroDivisionError''_''__build_class__''__debug__''__doc__''__import__''__loader__''__name__''__package__''__spec__''abs''all''any''ascii''bin''bool''bytearray''bytes''callable''chr''classmethod''compile''complex''copyright''credits''delattr''dict''dir''divmod''enumerate''eval''exec''exit''filter''float''format''frozenset''getattr''globals''hasattr''hash''help''hex''id''input''int''isinstance''issubclass''iter''len''license''list''locals''map''max''memoryview''min''next''object''oct''open''ord''pow''print''property''quit''range''repr''reversed''round''set''setattr''slice''sorted''staticmethod''str''sum''super''tuple''type''vars''zip']

3. 查看某个函数的文档帮助信息

可以用函数help(函数)来查看某个函数的文档帮助信息。

例子:

1

2

3

4

5

6

7

8

9

>>>help(sum)

Help on built-in function sum in module builtins:

sum(iterable, start=0/)   

       

    Return the sum of a 'start' value (default: 0) plus an iterable of numbers      When the iterable is empty, return the start value.    

       

    This function is intended specifically for use with numeric values and may    reject non-numeric types.

可以直接在提示符下输入help(),然后输入某个模块或函数名得到详细的帮助信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值