Python内置函数

id()

返回对象的唯一标识,CPython返回内存地址

In [12]: a=1

In [13]: id(a)
Out[13]: 1444225744

hash()

返回一个不可变对象的哈希值

In [14]: hash(a)
Out[14]: 1

In [17]: a=[1,2]

In [18]: hash(a)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-57b555d30865> in <module>
----> 1 hash(a)

TypeError: unhashable type: 'list'

input([prompt])

阻塞函数,接收输入,返回一个字符串

In [19]: input('inout a num: ')
inout a num: 1
Out[19]: '1'

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

打印输出,默认空格分隔、换行符结尾,返回值是None

In [21]: ret=print(1)
1

In [22]: type(ret)
Out[22]: NoneType

len(obj, /)

返回容器类型的元素个数

In [24]: print(len(range(2)))
2

abs(x, /)

In [33]: abs(-1)
Out[33]: 1

max(),min()

max(iterable, *[, default=obj, key=func]) -> value 返回可迭代对象中的最大或最小值
max(arg1, arg2, *args, *[, key=func]) -> value  返回参数中最大值或最小值

In [28]: max(1,2,['a','b'],('中国'),key=str)
Out[28]: '中国'

In [29]: max(1,2,['a','b'],key=str)
Out[29]: ['a', 'b']

range(start, stop[, step]) -> range object

返回从start 到stop-1的可迭代对象,step为步长

In [43]: for i in range(3,10,3):
    ...:     print(i)
    ...:
3
6
9

pow(x,y)

取x**y

In [34]: pow(5,2)
Out[34]: 25

divmod(x,y) 

取模,返回tuple(x//y, x%y)

In [44]: divmod(25,5)
Out[44]: (5, 0)

In [45]: divmod(25,4)
Out[45]: (6, 1)

sum(iteable[,start])

对只包含数值元素可迭代对象中的所有元素求和

In [49]: sum(range(3))
Out[49]: 3

chr(i)

对给定范围的整数返回对应的字符

In [52]: chr(20013)
Out[52]: ''

In [54]: import random
In [57]: chr(random.choice(range(97,123)))
Out[57]: 'e'

ord(c)

返回字符对应的整数

In [59]: ord('a')
Out[59]: 97

In [60]: ord('')
Out[60]: 22269

sorted(iterable, key=None, reverse=False)

以指定的函数key,对可迭代对象进行排序,返回一个列表。默认升序

In [65]: sorted({'abc':1,'bca':2,'def':3},reverse=False)
Out[65]: ['abc', 'bca', 'def']

In [66]: sorted({'abc':1,'bca':2,'def':3},reverse=True)
Out[66]: ['def', 'bca', 'abc']

 reversed(seq)

返回反转元素的迭代器

In [1]: reversed([1,2,5,2,7,3])
Out[1]: <list_reverseiterator at 0x2005671ca58>

In [2]: nl=reversed([1,2,5,2,7,3])

In [4]: next(nl)
Out[4]: 3

In [5]: next(nl)
Out[5]: 7

In [6]: next(nl)
Out[6]: 2

In [7]: next(nl)
Out[7]: 5

enumerate(seq, start=0)

对一个序列进行迭代,返回索引和元素组成的二元组,start表示索引的开始,默认为0

In [17]: for x in enumerate([1,2,5,3]):
    ...:     print(x)
    ...:
(0, 1)
(1, 2)
(2, 5)
(3, 3)

In [18]: a=enumerate([1,2,5,3])

In [19]: print(a)
<enumerate object at 0x0000020056917DC8>

iter(iterable)、next(iterator)

iter将一个可迭代对象封装为一个迭代器,next将一个迭代器进行取值,如果取到尽头,会跑出StopIteration异常

In [21]: itr=iter(range(4))

In [22]: next(itr)
Out[22]: 0

zip(*iterables)

将多个可迭代对象合并在一起,返回迭代器。每次从不同对象中取出一个元素组成一个元组,多余的元素会丢弃

In [25]: for x in zip(range(3),[1,2,4,7]):
    ...:     print(x)
    ...:
(0, 1)
(1, 2)
(2, 4)
In [28]: dict(zip(range(3),range(10)))
Out[28]: {0: 0, 1: 1, 2: 2}

exec(source, globals=None, locals=None, /)

该函数只传入一个参数时,会污染命名空间

In [30]: from math import sqrt

In [31]: exec("sqrt = 1")

In [32]: sqrt(4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-317e033d29d5> in <module>
----> 1 sqrt(4)

TypeError: 'int' object is not callable

所以一般会向其传入一个命名空间,通常是一个字典

In [33]: from math import sqrt

In [34]: scope={}

In [35]: exec("sqrt = 1",scope)

In [37]: scope['sqrt']
Out[37]: 1

In [38]: sqrt(4)
Out[38]: 2.0

当打印scope的时候发现scope包含了很多内容,因为自动在其中加入了所有内置函数值的字典_builtins_

In [39]: len(scope)
Out[39]: 2

In [40]: scope.keys()
Out[40]: dict_keys(['sqrt', '__builtins__'])
原文地址:https://www.cnblogs.com/zh-dream/p/13759427.html