First, built-in functions
1. Mathematical Operation class
ABS: Calculating the absolute value of a number
Divmod: Returns the quotient and remainder of two values, which can be used to calculate the number of pages
>>> Divmod (5,2) (2, 1)
Max: Returns the maximum value of an element in an iterative object or the maximum value of all parameters
Syntax: Max (Iterable,key,default)
1salaries={2 'Egon': the,3 'Alex':100000000,4 'Wupeiqi':10000,5 'Yuanhao': -6 }7 8Print (max (Salaries,key=lambda x:salaries[x]))
>>> Max (three-in-one) # Pass in 3 parameters take 3 3>>> max (' 1234 ') # Pass in an iterative object, take its maximum element value ' 4 ' >>> max ( -1,0) # The value defaults to the larger value 0>>> max ( -1,0,key = ABS) # passed in the absolute value function, then the parameter will be the absolute value and then take the larger person-1
Min: Returns the minimum value of an element in an iterative object or the minimum value of all parameters
>>> min (three-in-one) # incoming 3 parameters take 3 smaller 1>>> min (' 1234 ') # Pass in an iterative object, take its minimum element value ' 1 ' >>> min ( -1,-2) # The value defaults to the lesser of the value -2>>> min ( -1,-2,key = ABS) # passed in the absolute value function, then the parameter will be the absolute value and then take the smaller person-1
POW: Returns a power value of two numeric values or a modulo value with a specified integer
>>> Pow (2,3) #相当于2 **3>>> pow (2,3,5) #相当于 pow (2,3)%5
Round: Rounding evaluation of floating-point number
>>> round (1.1314926,1) 1.1>>> round (1.1314926,5) 1.13149
Sum: Sums each element in an iterative object that the element type is numeric
# Incoming Iteration object >>> sum ((1,2,3,4))# element type must be numeric >>> sum ((1.5,2.5,3.5,4.5)) 12.0> >> sum ((1,2,3,4),-10) 0
2. Type conversion class
BOOL: Creates a new Boolean value based on the logical value of the parameter passed in
>>> bool () #未传入参数False >>> bool (0) #数值0, empty sequence equivalent is falsefalse>>> bool (1) True
INT: Creates a new integer based on the parameters passed in
Float: Creates a new floating-point number based on the parameters passed in
Complex: Creates a new complex number based on the parameters passed in
#当两个参数都不提供时, returns the plural 0j. #传入字符串创建复数 #传入数值创建复数(1+2j)
STR: Returns the string representation of an object (to the user)
ByteArray: Creates a new byte array based on the parameters passed in
>>> ByteArray (' Chinese ', ' utf-8 ') ByteArray (b ' \xe4\xb8\xad\xe6\x96\x87 ')
Bytes: Creates a new immutable byte array based on the parameters passed in
>>> bytes (' Chinese ', ' utf-8 ') B ' \xe4\xb8\xad\xe6\x96\x87 '
Ord: Returns the integer corresponding to the Unicode character
>>> Ord (' a ') 97
CHR: Returns the Unicode character corresponding to the integer
>>> chr #参数类型为整数 ' a '
Bin: Converting integers to 2 binary strings
>>> Bin (3) ' 0B11 '
Oct: Converting integers to 8 binary string
>>> Oct (Ten) ' 0o12 '
Hex: Converts integers to 16 binary strings
>>> Hex ' 0xf '
Tuple: Create a new tuple based on the parameters passed in
>>> tuple () #不传入参数, create an empty tuple () >>> tuple (' 121 ') #传入可迭代对象. Create a new tuple with its elements (' 1 ', ' 2 ', ' 1 ')
List: Create a new list based on the parameters passed in
>>>list () # do not pass in parameters, create empty list [] >>> list (' ABCD ') # to pass in an iterative object, use its elements to create a new list [' A ', ' B ', ' C ', ' d ']
Dict: Create a new dictionary based on the parameters passed in
>>> dict () # Returns an empty dictionary when no parameters are passed in. {}>>> dict (A = 1,b = 2) # You can create a dictionary by passing in a key-value pair. {' B ': 2, ' a ': 1}>>> dict ([' A ', ' B '],[1,2]) # You can pass in a mapping function to create a dictionary. {' B ': 2, ' a ': 1}>>> dict ((' A ', 1), (' B ', 2)) # You can pass in an iterative object creation dictionary. {' B ': 2, ' A ': 1}
Set: Creates a new collection based on the parameters passed in
>>>set () # do not pass in parameters, create empty collection set () >>> a = set (range (10)) # Pass in an iterative object, create a collection >>> a{0, 1, 2, 3, 4, 5, 6, 7, 8 , 9}
Frozenset: Creates a new immutable collection based on the parameters passed in
>>> A = Frozenset (range) >>> Afrozenset ({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
Enumerate: Creating an Enumeration object based on an iterative object
start=1)) #指定起始值[(1, ' Spring '), (2, ' Summer '), (3, ' Fall '), (4, ' Winter ')]
Range: Creates a new Range object based on the parameters passed in
>>> a = range (Ten) >>> B = Range (1,10) >>> c = Range (1,10,3) >>> a,b,c # Output A,b,c (range (0 , Range (1, ten), Range (1, 3)) >>> list (a), list (b), List (c) # Outputs the elements of A,b,c ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [ 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 4, 7])
ITER: Creates a new, iterative object based on the parameters passed in
>>> a = iter (' ABCD ') #字符串序列 >>> A<str_iterator object at 0x03fb4fb0>>>> next (a) ' a '
Slice: Creates a new tile object based on the parameters passed in
>>> C1 = Slice (5) # definition c1>>> c1slice (none, 5, none) >>> C2 = Slice (2,5) # definition c2>>> c2slic E (2, 5, None) >>> C3 = Slice (1,10,3) # definition c3>>> c3slice (1, 10, 3)
Super: Creates a new subclass and a proxy object for the parent class relationship based on the parameters passed in
3. Sequence Operation class
All: Determines whether each element of an iterative object is a true value
>>> all ([truetrue>>>]) #列表中每个元素逻辑值均为True, return to all ([0,1,2]) #列表中0的逻辑值为False, return falsefalse>> > All (()) #空元组True >>> all ({}) #空字典True
Any: Determines whether an element that iterates over an object has an element that is a true value
>>> any ([0,1,2]) #列表元素有一个为True, returns the Truetrue>>> any ([0,0]) #列表元素全部为False, then returns falsefalse>>> Any ([]) #空列表False >>> any ({}) #空字典False
FIlter: Filters The elements of an iterative object using the specified method
>>> A = List (range (1,10)) #定义序列 >>> a[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> def if_odd (x): #定义奇数判断函数
return x%2==1>>> List (filter (if_odd,a)) #筛选序列中的奇数 [1, 3, 5, 7, 9]
Map: Creates a new iterative object by using the specified method to function the elements of each iteration object that is passed in
>>> a = map (ord, ' ABCD ') >>> A<map object at 0x03994e50>>>> list (a) [97, 98, 99, 100]
# Map: Map
l=[1,2,3,4]
M=map (Lambda x:x**2,l)
Print (list (m))
names=[' Alex ', ' Wupeiqi ', ' Yuanhao ']
Print (list (map (lambda item:item+ ' _sb ', names)))
Next: Returns the next element value in an iterative object
>>> a = iter (' ABCD ') >>> next (a) ' A ' #传入default参数后, if the iterator object has an element that does not return, then returns its element value, if all elements have returned, Returns the default specified defaults without throwing the stopiteration exception >>> next (A, ' e ') ' E '
Reversed: Reversing a sequence to generate a new iterative object
>>> A = reversed (range (10)) # Incoming Range object >>> A # type becomes iterator <range_iterator object at 0X035634E8>>&G T;> list (a) [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Sorted: Sorts objects that can be iterated, returning a new list
>>> a = [' A ', ' B ', ' d ', ' C ', ' B ', ' A ']>>> a[' A ', ' B ', ' d ', ' C ', ' B ', ' A ']>>> sorted (a) # Default by character ASCII code Sort [' A ', ' B ', ' A ', ' B ', ' C ', ' d ']>>> sorted (A,key = str.lower) # Convert to lowercase and then sort, ' a ' and ' a ' values like ' B ' and ' B ' values [' a ', ' a ', ' B ', ' B ' ', ' C ', ' d ']
L=[1,2,4,9,-1]
Print (sorted (l)) #从小到大
Print (sorted (l,reverse=true)) #从大到小
Zip: Aggregates an element in the same position in each iterator passed in, returning a new tuple type iterator
>>> x = [4] #长度3 >>> y = [4,5,6,7,8] #长度5 >>> list (Zip (x, y) #) # Take the minimum length 3[(1, 5), (3, 6)]
Reduce: Merging
From Functools import reduceprint (redu CE (Lambda x,y:x+y,range (100), 100))
Filter: Filtering
names=[' ALEX_SB ', ' yuanhao_sb ', ' wupeiqi_sb ', ' Egon ']print (list (filter (Lambda name:name.endswith (' _sb '), names)))
4. Object Operation class
ID: Returns the unique identifier of the object
>>> a = ' Some text ' >>> ID (a) 69228568
Hash: Gets the hash value of the object
>>> hash (' Good Good study ') 1032709256
Type: Returns the types of objects, or creates a new type based on the parameters passed in
>>> type (1) # Returns the object's <class ' int ' >
Len: Returns the length of the object
Second, anonymous function
An anonymous function is one that does not require an explicit function to be specified
#这段代码def Calc (n): return N**nprint (calc) #换成匿名函数calc = Lambda n:n**nprint (Calc (10))
anonymous function Application:
L=[3,2,100,999,213,1111,31121,333]print (Max (l)) dic={' K1 ': Ten, ' K2 ': +, ' K3 ': 30}print (Max (DIC)) print (Dic[max (DIC, Key=lambda K:dic[k])
res = map (lambda x:x**2,[1,5,7,4,8]) for I in Res: print (i) Output 125491664
salaries={' Egon ':, ' Alex ': 100000000, ' Wupeiqi ': 10000, ' Yuanhao ': 2000}print (max (Salaries,key=lambda x:salaries[ X]))
Third, recursion
Recursive invocation:
In the process of invoking a function, the function itself is called directly or indirectly
#递归去列表的值l =[1,[2,3,[4,5,[6,7,[8,9,[10,11,[12,13]]]]]]]def func (L): for i in L: if Isinstance (i,list): Func (i) else: print (i) Func (l)
Note: Built-in functions from: http://blog.csdn.net/oaa608868/article/details/53506188
Python Basics (10) _ Built-in functions, anonymous functions, recursion