Python基础教程

017_Python内置函数

Python内置函数

Python的内置函数在编程过程中使用频率非常高,必须掌握,或者说使用多了会被动掌握。

官方手册(https://docs.python.org/zh-cn/3/library/functions.html)给出了内置函数的列表

内置函数
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
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()

里面有很多函数我们已经使用过,还有一些没有见过的或者记忆的函数,可以在使用时再查询。

下面讲解几个很基础的函数。

help()函数

help()函数,启动内置的帮助系统。

如果实参是一个字符串,则在模块、函数、类、方法、关键字或文档主题中搜索该字符串,并在控制台上打印帮助信息。如果实参是其他任意对象,则会生成该对象的帮助页。

语法

help([object])

示例,在代码中输入如下内容

help(print)

运行结果如下。可以看到print函数的使用说明。

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/atestpkg/t42.py
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

Process finished with exit code 0

dir()函数

dir()函数,得到一个对象的属性列表(即内容列表)。

语法

dir([object])

示例如下

print(dir(list))

运行结果如下。可以看到list对象中包含很多内容,比如append,remove等方法。

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/atestpkg/t42.py
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Process finished with exit code 0

str()函数与repr()函数

str()函数和repr()函数,都是把其他类型数据转换成字符串类型。

二者功能相似,字符串的结果有时存在部分区别。

str()函数返回的字符串偏向于给程序员使用。

repr()函数返回的结果偏向于给机器执行,相当于原样输出。

示例1

alist=[1,2,3]

print(str(alist))
print(repr(alist))

运行结果,相同。

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/atestpkg/t42.py
[1, 2, 3]
[1, 2, 3]

Process finished with exit code 0

示例2

astr="hello nworld"

print(str(astr))
print("------------------------")
print(repr(astr))

运行结果,不同。

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/atestpkg/t42.py
hello 
world
------------------------
'hello nworld'

Process finished with exit code 0

type()函数与isinstance()函数

type()函数与isinstance()函数都可用于判断数据类型。

推荐使用 isinstance() 内置函数来检测对象的类型,因为它会考虑子类的情况。

语法

type(object)
isinstance(object, classinfo)

示例

alist=[1,2,3]

print(type(alist))
print(isinstance(alist,list))
print(isinstance(alist,int))

运行结果

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/atestpkg/t42.py
<class 'list'>
True
False

Process finished with exit code 0

id()函数

id() 函数返回对象的唯一标识符,标识符是一个整数。

CPython 中 id() 函数用于获取对象的内存地址。

语法

id(object)

示例

alist=[1,2,3]

print(id(alist))
print(id(alist[0]))
print(id(alist[1]))
print(id(alist[2]))

运行结果

C:UsershccmaAnaconda3python.exe E:/wkp01/p00/test01/atestpkg/t42.py
2573300421064
140722206450064
140722206450096
140722206450128

Process finished with exit code 0
这篇文章对您有用吗?

我们要如何帮助您?