Python Common built-in function summary _python

Source: Internet
Author: User
Tags chr ord zip

First, mathematics related

1, Absolute Value: ABS (-1)
2, the maximum minimum value: Max ([1,2,3]), MIN ([1,2,3])
3, Sequence length: Len (' abc '), Len ([1,2,3]), Len ((1,2,3))
4, take the mold: Divmod (5,2)//(2,1)
5, the Powers: Pow (2,3,4)//2**3/4
6, Floating Points: round (1)//1.0

Second, function-related

1, whether the function can be called: callable (funcname), note that funcname variable to define
2. Type judgment: Isinstance (X,list/int)
3. Comparison: CMP (' hello ', ' hello ')
4. Fast generation Sequence: (x) range ([Start,] stop[, step])

Third, 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 character corresponding to the ASC code number number, such as Ord (' A ') return 65

Four, string processing

1, initial capital: Str.capitalize

Copy Code code as follows:

>>> ' Hello '. Capitalize ()

' Hello '
2. String Replacement: Str.replace
Copy Code code as follows:

>>> ' Hello '. Replace (' l ', ' 2 ')
' He22o '

You can pass three arguments, and the third parameter is the number of replacements

3. String cutting: Str.split

Copy Code code as follows:

>>> ' Hello '. Split (' L ')
[' He ', ', ', ' O ']

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

All three of these methods can be introduced into the string module and then invoked in a string.xxx manner.

V. Sequence processing functions

1. Len: Sequence length
2, Max: The maximum value in the sequence
3, min: Minimum value
4. Filter: Filtration Sequence

Copy Code code as follows:

>>> filter (lambda x:x%2==0, [1,2,3,4,5,6])
[2, 4, 6]

5, Zip: Parallel traversal

Copy Code code as follows:

>>> name=[' Jim ', ' Tom ', ' Lili '
>>> age=[20,30,40]
>>> tel=[' 133 ', ' 156 ', ' 189 ']
>>> Zip (Name,age,tel)
[(' Jim ', ' 156 '], (' Tom ', ', ') ', (' Lili ', 40, ' 189 ')]

Note that if the sequence length is not the same, it will be the following result:
Copy Code code as follows:

>>> name=[' Jim ', ' Tom ', ' Lili '
>>> age=[20,30,40]
>>> tel=[' 133 ', ' 170 ']
>>> Zip (Name,age,tel)
[(' Jim, ', '], (' Tom ', 30, ' 170 ')]

6, Map: Parallel traversal, can accept the parameters of a function type
Copy 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 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 , 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, 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 , 95, 96, 97, 98, 99, 100]
>>> reduce (lambda x,y:x+y,l)
5050

Related Article

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.