程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Here comes welfare! The most complete summary of Python built-in functions is recommended to collect!

編輯:Python

1 abs()

A module of absolute or complex values

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

2 all()

Accept an iterator , If all elements of an iterator are true , Then the return True, Otherwise return to False

In [2]: all([1,0,3,6])
Out[2]: False
In [3]: all([1,2,3])
Out[3]: True

3 any()

Accept an iterator , If one of the elements in the iterator is true , Then the return True, Otherwise return to False

In [4]: any([0,0,0,[]])
Out[4]: False
In [5]: any([0,0,1])
Out[5]: True

4 ascii()

 Call the object's repr() Method , Get the return value of the method
In [30]: class Student():
...: def __init__(self,id,name):
...: self.id = id
...: self.name = name
...: def __repr__(self):
...: return 'id = '+self.id +', name = '+self.name
In [33]: print(xiaoming)
id = 001, name = xiaoming
In [34]: ascii(xiaoming)
Out[34]: 'id = 001, name = xiaoming'

5 dict()

Create data dictionary

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()

Returns the variable in the current range without parameters , List of types of methods and definitions ; Returns the properties of the parameter when it has parameters , Method list .

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()

Take quotient and remainder respectively

In [97]: divmod(10,3)
Out[97]: (3, 1)

8 isinstance(object, classinfo)

Judge object Is it a class classinfo Example , Is to return 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)

If class yes classinfo Subclasses of classes , return True:

In [27]: class undergraduate(Student):
...: def studyClass(self):
...: pass
...: def attendActivity(self):
...: pass
...:
In [28]: issubclass(undergraduate,Student)
Out[28]: True
In [29]: issubclass(object,Student)
Out[29]: False
In [30]: issubclass(Student,object)
Out[30]: True
If class yes classinfo Subclass of an element in a tuple , And will return to True
In [26]: issubclass(int,(int,float))
Out[26]: True

10 iter(object, sentinel)

Returns an iteratable object , sentinel Omission

In [72]: lst = [1,3,5]
In [73]: for i in iter(lst):
...: print(i)
...:
1
3
5

sentinel It is understood as the sentinel of iterative objects , Once you iterate over this element , Immediately terminate :

In [81]: class TestIter(object):
...: def __init__(self):
...: self.l=[1,3,2,3,4,5]
...: self.i=iter(self.l)
...: def __call__(self): # Defined __call__ The instance of the class of the method is callable
...: item = next(self.i)
...: print ("__call__ is called,which would return",item)
...: return item
...: def __iter__(self): # Support iterative protocol ( It is defined as __iter__() function )
...: 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 1
1
__call__ is called,which would return 3

11 max(iterable,*, key, default)

Return maximum :

In [99]: max(3,1,4,2,1)
Out[99]: 4
In [100]: max((),default=0)
Out[100]: 0
In [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)

Return minimum

13 memoryview(obj)

Returns the... Created by the given argument “ Memory view ” object , Python The code accesses the internal data of an object , As long as the object supports Buffer protocol Without copying

14 next(iterator,, default)

Returns the next element of the iterable object

In [129]: it = iter([5,3,4,1])
In [130]: next(it)
Out[130]: 5
In [131]: next(it)
Out[131]: 3
In [132]: next(it)
Out[132]: 4
In [133]: next(it)
Out[133]: 1
In [134]: next(it,0) # End of iteration , The default return value is 0
Out[134]: 0
In [135]: next(it)
----------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-135-bc1ab118995a> in <module>
----> 1 next(it)
StopIteration:

15 object()

Returns a new object without a feature .object Is the base class of all classes .

In [137]: o = object()
In [138]: type(o)
Out[138]: object

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved