将使用频繁的代码段封装起来,并给它起一个名字,以后使用的时候只要知道名字就可以,这就是函数。函数就是一段封装好的、可以重复使用的代码,它使得我们的程序更加模块化,不需要编写大量重复的代码。
内置函数和标准库函数是不一样的。在 Python 2.x 中,print 是一个关键字;到了 Python 3.x 中,print 变成了内置函数。
除了 print() 函数,Python 解释器还提供了更多内置函数,下表列出了 Python 3.x 中的所有内置函数。| 内置函数 | ||||
|---|---|---|---|---|
| abs() | delattr() | hash() | memoryview() | set() |
| all() | dict() | help() | min() | setattr() |
| any() | dir() | hex() | next() | slicea() |
| ascii() | divmod() | id() | object() | sorted() |
| bin() | enumerate() | input() | oct() | staticmethod() |
| bool() | eval() | int() | open() | str() |
| breakpoint() | exec() | isinstance() | ord() | sum() |
| bytearray() | filter() | issubclass() | pow() | super() |
| bytes() | float() | iter() | print() | tuple() |
| callable() | format() | len() | property() | type() |
| chr() | frozenset() | list() | range() | vars() |
| classmethod() | getattr() | locals() | repr() | zip() |
| compile() | globals() | map() | reversed() | __import__() |
| complex() | hasattr() | max() | round() | |
>>> print = "http://c.biancheng.net/python/" #将print作为变量名
>>> print("Hello World!") #print函数被覆盖,失效
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
print("Hello World!")
TypeError: 'str' object is not callable
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有