Summary of common Python built-in functions, python built-in functions
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: divmod (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
Copy codeThe Code is as follows:
>>> 'Hello'. capitalize ()
'Hello'
2. String replacement: str. replace
Copy codeThe 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
Copy codeThe 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
Copy codeThe Code is as follows:
>>> Filter (lambda x: x % 2 = 0, [1, 2, 3, 4, 5, 6])
[2, 4, 6]
5. zip: Parallel Traversal
Copy codeThe 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:
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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