1 abs()
绝对值或复数的模
In [1]: abs(-6)Out[1]: 6
2 all()
承受一个迭代器,如果迭代器的所有元素都为真,那么返回True,否则返回False
In [2]: all([1,0,3,6])Out[2]: FalseIn [3]: all([1,2,3])Out[3]: True
3 any()
承受一个迭代器,如果迭代器里有一个元素为真,那么返回True,否则返回False
In [4]: any([0,0,0,[]])Out[4]: FalseIn [5]: any([0,0,1])Out[5]: True
4 ascii()
调用对象的repr() 办法,取得该办法的返回值In [30]: class Student(): ...: def __init__(self,id,name): ...: self.id = id ...: self.name = name ...: def __repr__(self): ...: return 'id = '+self.id +', name = '+self.nameIn [33]: print(xiaoming)id = 001, name = xiaomingIn [34]: ascii(xiaoming)Out[34]: 'id = 001, name = xiaoming'
5 dict()
创立数据字典
In [92]: dict()Out[92]: {}In [93]: dict(a='a',b='b')Out[93]: {'a': 'a', 'b': 'b'}In [94]: dict(zip(['a','b'],[1,2]))Out[94]: {'a': 1, 'b': 2}In [95]: dict([('a',1),('b',2)])Out[95]: {'a': 1, 'b': 2}
6 dir()
不带参数时返回以后范畴内的变量,办法和定义的类型列表;带参数时返回参数的属性,办法列表。
In [96]: dir(xiaoming)Out[96]:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
7 divmod()
别离取商和余数
In [97]: divmod(10,3)Out[97]: (3, 1)
8 isinstance(object, classinfo)
判断object是否为类classinfo的实例,是返回true
In [20]: class Student(): ...: ...: def __init__(self,id,name): ...: ...: self.id = id ...: ...: self.name = name ...: ...: def __repr__(self): ...: ...: return 'id = '+self.id +', name = '+self.name ...:In [21]: xiaoming = Student('001','xiaoming')In [22]: isinstance(xiaoming,Student)Out[22]: True
9 issubclass(class, classinfo)
如果class是classinfo类的子类,返回True:
In [27]: class undergraduate(Student): ...: def studyClass(self): ...: pass ...: def attendActivity(self): ...: pass ...:In [28]: issubclass(undergraduate,Student)Out[28]: TrueIn [29]: issubclass(object,Student)Out[29]: FalseIn [30]: issubclass(Student,object)Out[30]: True如果class是classinfo元组中某个元素的子类,也会返回TrueIn [26]: issubclass(int,(int,float))Out[26]: True
10 iter(object, sentinel)
返回一个可迭代对象, sentinel可省略
In [72]: lst = [1,3,5]In [73]: for i in iter(lst): ...: print(i) ...:135
sentinel 了解为迭代对象的哨兵,一旦迭代到此元素,立刻终止:
In [81]: class TestIter(object): ...: def __init__(self): ...: self.l=[1,3,2,3,4,5] ...: self.i=iter(self.l) ...: def __call__(self): #定义了__call__办法的类的实例是可调用的 ...: item = next(self.i) ...: print ("__call__ is called,which would return",item) ...: return item ...: def __iter__(self): #反对迭代协定(即定义有__iter__()函数) ...: print ("__iter__ is called!!") ...: return iter(self.l) ...:In [82]: t = TestIter() ...: t1 = iter(t, 3) ...: for i in t1: ...: print(i) ...:__call__ is called,which would return 11__call__ is called,which would return 3
11 max(iterable,*[, key, default])
返回最大值:
In [99]: max(3,1,4,2,1)Out[99]: 4In [100]: max((),default=0)Out[100]: 0In [89]: di = {'a':3,'b1':1,'c':4}In [90]: max(di)Out[90]: 'c'In [102]: a = [{'name':'xiaoming','age':18,'gender':'male'},{'name':' ...: xiaohong','age':20,'gender':'female'}]In [104]: max(a,key=lambda x: x['age'])Out[104]: {'name': 'xiaohong', 'age': 20, 'gender': 'female'}
12 min(iterable,*[, key, default])
返回最小值
13 memoryview(obj)
返回由给定实参创立的“内存视图”对象, Python 代码拜访一个对象的外部数据,只有该对象反对 缓冲区协定 而无需进行拷贝
14 next(iterator,[, default])
返回可迭代对象的下一个元素
In [129]: it = iter([5,3,4,1])In [130]: next(it)Out[130]: 5In [131]: next(it)Out[131]: 3In [132]: next(it)Out[132]: 4In [133]: next(it)Out[133]: 1In [134]: next(it,0) #迭代到头,默认返回值为0Out[134]: 0In [135]: next(it)----------------------------------------------------------------------StopIteration Traceback (most recent call last)<ipython-input-135-bc1ab118995a> in <module>----> 1 next(it)StopIteration:
15 object()
返回一个没有特色的新对象。object 是所有类的基类。
In [137]: o = object()In [138]: type(o)Out[138]: object