This article mainly introduces the summary of commonly used built-in functions in Python. This article lists commonly used built-in functions such as mathematics, function, type conversion, string processing, and sequence processing functions. For more information, see
I. mathematical problems
1. absolute value: abs (-1)
2. maximum and minimum values: max ([1, 2, 3]), min ([1, 2, 3])
3. sequence length: len ('ABC'), len ([1, 2, 3]), len (1, 2, 3 ))
4. modulo: pmod (5, 2) // (2, 1)
5. multiplication: pow (2, 3, 4) // 2 ** 3/4
6. Floating Point: round (1) // 1.0
II. Functions
1. callable function: callable (funcname). Note that the funcname variable must be defined.
2. type determination: isinstance (x, list/int)
3. Comparison: cmp ('hello', 'Hello ')
4. fast generation sequence: (x) range ([start,] stop [, step])
III. type conversion
1. int (x)
2. long (x)
3. float (x)
4. complex (x) // plural
5. str (x)
6. list (x)
7. tuple (x) // tuples
8. hex (x)
9. oct (x)
10. chr (x) // returns the character corresponding to x. for example, chr (65) returns 'A'
11. ord (x) // return the ASC code number corresponding to the character. for example, ord ('A') returns 65.
IV. String Processing
1. uppercase letters: str. capitalize
The code is as follows:
>>> 'Hello'. capitalize ()
'Hello'
2. string replacement: str. replace
The code is as follows:
>>> 'Hello'. replace ('L', '2 ')
'He22o'
Three parameters can be passed, and the third parameter is the number of replicas.
3. String cutting: str. split
The code is as follows:
>>> 'Hello'. split ('L ')
['Hes', '', 'O']
Two parameters can be passed, and the second parameter is the cut count.
The above three methods can be introduced into the String Module, and then called in the string. xxx method.
V. sequence processing functions
1. len: sequence length
2. max: maximum value in the sequence
3. min: minimum value
4. filter: filter sequence
The code is as follows:
>>> Filter (lambda x: x % 2 = 0, [1, 2, 3, 4, 5, 6])
[2, 4, 6]
5. zip: parallel traversal
The code is as follows:
>>> Name = ['Jim ', 'Tom', 'Lili']
>>> Age = [20, 30, 40]
>>> Tel = ['000000', '000000', '000000']
>>> Zip (name, age, tel)
[('Jim ', 20, '000000'), ('Tom', 30, '000000'), ('Lili', 40, '000000')]
Note: If the sequence length is different, the result is as follows:
The code is as follows:
>>> Name = ['Jim ', 'Tom', 'Lili']
>>> Age = [20, 30, 40]
>>> Tel = ['000000', '000000']
>>> Zip (name, age, tel)
[('Jim ', 20, '123'), ('Tom', 30, '123')]
6. map: parallel traversal, accepting a function-type parameter
The code is as follows:
>>> A = [1, 3, 5]
>>> B = [2, 4, 6]
>>> Map (None, a, B)
[(1, 2), (3, 4), (5, 6)]
>>> Map (lambda x, y: x * y, a, B)
[2, 12, 30]
7. reduce: Merge
The code is as follows:
>>> L = range (1,101)
>>> L
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100]
>>> Reduce (lambda x, y: x + y, l)
5050