Python study Note 6: built-in functions and python Study Notes

Source: Internet
Author: User

Python study Note 6: built-in functions and python Study Notes
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

>>> 'hello'.capitalize()'Hello'

2. String replacement: str. replace

>>> 'hello'.replace('l','2')'he22o'

Three parameters can be passed, and the third parameter is the number of replicas.

3. String cutting: str. split

>>> 'hello'.split('l')['he', '', '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

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

5. zip: Parallel Traversal

>>> name=['jim','tom','lili']>>> age=[20,30,40]>>> tel=['133','156','189']>>> zip(name,age,tel)[('jim', 20, '133'), ('tom', 30, '156'), ('lili', 40, '189')]

Note: If the sequence length is different, the result is as follows:

>>> name=['jim','tom','lili']>>> age=[20,30,40]>>> tel=['133','170']>>> zip(name,age,tel)[('jim', 20, '133'), ('tom', 30, '170')]

6. map: Parallel traversal, accepting a function-type parameter

>>> 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

>>> 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

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.