Python内置函数(1)
Python有着丰富的内置函数,包括异常类型等100多个BIF,我们这里讨论的是除去异常BIF的剩余的72个。将分为7个小节来阐述。
1.abs(x)
返回参数x的绝对值,如果x是复数,就返回其模的大小
>>> abs(3) 3 >>> abs(-3.5) 3.5 >>> abs(3+4j) 5.0 >>>
2.all(iterable)
如果对于iterable中的每一个元素x,bool(x)都为真,即iterable中所有元素都为真,就返回True,只要有一个为假就返回False
>>> all([1,'a',[1]]) True >>> all([True,False,1]) False >>>
3.any(iterable)
如果对于iterable中的每一个元素x,bool(x)只要有一个为True,就返回True,否则返回False
>>> any([1,0,False]) True >>> any(['',0,False]) False >>>
4.dir(object)
返回object的属性与方法
>>> dir(set) ['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute __', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__' , '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub __', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'dif ference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update'] >>>
5.id(object)
返回一个对象的内存地址
>>> str1 = 'Keys' >>> id(str1) 720667107880 >>> id(100) 1772213008 >>>
6.len(object)
返回一个容器中的项目数量
>>> str1 = 'Keys' >>> list1 = [1,2,3,4] >>> len(str1) 4 >>> len(list1) 4 >>>
7.list(iterable)
将一个iterable对象转换成一个列表或创建一个新列表
>>> a='Keys' >>> list(a) ['K', 'e', 'y', 's'] >>> b=(1,2,3,4) >>> list(b) [1, 2, 3, 4] >>>
8.max(...)
如果只有一个可迭代参数,则返回其最大项,如果有两个或者更多参数,返回其最大的参数
>>> list1 = [2,8,9,7] >>> max(list1) 9 >>> str1 = 'Keys' >>> max(str1) 'y' >>> max(1,2,3) 3 >>>
9.min(...)
如果只有一个可迭代参数,则返回其最小项,如果有两个或者更多参数,返回其最小的参数
>>> min(1,2,3) 1 >>> str1 = 'Keys' >>> min(str1) 'K' >>> list1 = [2,8,9,7] >>> min(list1) 2 >>>
10.pow(x,y,z=None)
如果只有x,y两个参数,返回x**y,如果有x,y,z三个参数,返回x**y % z
>>> pow(2,3) 8 >>> pow(2,3,7) 1 >>>