Python common built-in functions summary

Source: Internet
Author: User

First, mathematics-related

1, Absolute Value: ABS (-1)
2, Maximum minimum: max ([+ +]), min ([[+])
3. Sequence Length: Len (' abc '), Len ([+]), Len ((+))
4, take the mold: Divmod (5,2)
5. exponentiation: Pow (2,3)
6, floating point: round (1)

Second, function-related

1. Whether the function can be called: callable (funcname), note that the funcname variable is to be defined
2. Type judgment: 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)//tuple
8, Hex (x)
9. Oct (x)
10, Chr (x)//return x corresponding character, such as Chr (65) return ' A '
11, Ord (x)//return the character corresponding to the ASC code number, such as Ord (' A ') return 65

Four, string processing

1, the first letter capital: str.capitalize

Copy the code code as follows:
>>> ' Hello '. Capitalize ()


' Hello '
2. String substitution: Str.replace

Copy the code code as follows:
>>> ' Hello '. Replace (' l ', ' 2 ')
' He22o '


Can pass three parameters, the third parameter is the number of replacements

3, String cutting: Str.split

Copy the code code as follows:
>>> ' Hello '. Split (' L ')
[' He ', ' ', ' o ']


Can pass two parameters, the second parameter is the number of cuts

The above three methods can be introduced into the string module, and then the call is made in string.xxx way.

Five, sequence processing function

1. Len: Sequence length
2, Max: Maximum value in a sequence
3, min: Minimum value
4. Filter: Filtering sequence

Copy the code code as follows:
>>> filter (lambda x:x%2==0, [1,2,3,4,5,6])
[2, 4, 6]

5. zip: Parallel traversal

Copy the code code as follows:
>>> name=[' Jim ', ' Tom ', ' Lili '
>>> age=[20,30,40]
>>> tel=[' 133 ', ' 156 ', ' 189 ']
>>> Zip (Name,age,tel)
[(' Jim ', ' 133 '), (' Tom ', ' A ', ' 156 '), (' Lili ', 40, ' 189 ')]


Note that if the sequence length is not the same, the result would be the following:

Copy the code code as follows:
>>> name=[' Jim ', ' Tom ', ' Lili '
>>> age=[20,30,40]
>>> tel=[' 133 ', ' 170 ']
>>> Zip (Name,age,tel)
[(' Jim ', ' 133 '), (' Tom ', 30, ' 170 ')]


6, Map: Parallel traversal, can accept the parameters of a function type

Copy the code code 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 the code code 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 , 95, 96, 97, 98, 99, 100]
>>> reduce (lambda x,y:x+y,l)
5050

Python common built-in functions summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.