Python beginners and advanced notes: Python built-in function summary, python advanced

Source: Internet
Author: User

Python beginners and advanced notes: Python built-in function summary, python advanced

Built-in functions
Common functions

1. Mathematical Correlation
• Abs (x)
Abs () returns the absolute value of a number. If a plural number is given, the return value is the modulo of the plural number.

Copy codeThe Code is as follows:
>>> Print abs (-100)
100
>>> Print abs (1 + 2j)
2.2360679775

• Divmod (x, y)
The divmod (x, y) function completes Division operations and returns the quotient and remainder.

Copy codeThe Code is as follows:
>>> Divmod (10, 3)
(3, 1)
>>> Divmod (9, 3) (3, 0)

• Pow (x, y [, z])
The pow () function returns the power of x as the base and y as the index. If the z value is given, this function calculates the y power value of x, which is the value of z modulo.

Copy codeThe Code is as follows:
>>> Print pow (2, 4)
16
>>> Print pow (2, 4, 2)
0
>>> Print pow (2.4, 3)
13.824

• Round (x [, n])
The round () function returns the rounding value of floating point x. If n is given, it indicates the number of digits rounded to the decimal point.

Copy codeThe Code is as follows:
>>> Round (3.333)
3.0
>>> Round (3)
3.0
>>> Round (5.9)
6.0

• Min (x [, y, z...])
The min () function returns the minimum value of a given parameter, which can be a sequence.

Copy codeThe Code is as follows:
>>> Min (1, 2, 3, 4)
1
>>> Min (1, 2, 3), (2, 3, 4 ))
(1, 2, 3)

• Max (x [, y, z...])
The max () function returns the maximum value of a given parameter, which can be a sequence.

Copy codeThe Code is as follows:
>>> Max (1, 2, 3, 4)
4
>>> Max (1, 2, 3), (2, 3, 4 ))
(2, 3, 4)

2. Sequence Correlation

• Len (object)-> integer
The len () function returns the length of the string and sequence.

Copy codeThe Code is as follows:
>>> Len ("aa ")
2
>>> Len ([1, 2])
2

• Range ([lower,] stop [, step])
The range () function generates a list of sequential integers based on parameters.

Copy codeThe Code is as follows:
>>> Range (10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Range (1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Range (1, 10, 2)
[1, 3, 5, 7, 9]

• Xrange ([lower,] stop [, step])
The xrange () function is similar to range (), but xrnage () returns an xrange object instead of creating a list.

Similar to the list, but the list value is calculated only when needed. when the list is large, this feature can save us memory.

Copy codeThe Code is as follows:
>>> A = xrange (10)
>>> Print a [0]
0
>>> Print a [1]
1
>>> Print a [2]
2

3. Objects and types
• Callable (object)
The callable () function is used to test whether the object is callable. If yes, 1 (true) is returned; otherwise, 0 (false) is returned ). Callable objects include functions, methods, code objects, classes, and class instances that have defined call methods.

Copy codeThe Code is as follows:
>>> A = "123"
>>> Print callable ()
False
>>> Print callable (chr)
True

• Cmp (x, y)
The cmp () function compares two objects, x and y, and returns an integer based on the comparison result. If x <y,-1 is returned. If x> y, 1 is returned, if x = y, 0 is returned.

Copy codeThe Code is as follows:
>>> A = 1
>>> B = 2
>>> C = 2
>>> Print cmp (a, B)
-1
>>> Print cmp (B,)
1
>>> Print cmp (B, c)
0

• Isinstance (object, class-or-type-or-tuple)-> bool
Test Object Type

Copy codeThe Code is as follows:
>>> A = 'isinstance Test'
>>> B = 1234
>>> Isinstance (a, str)
True
>>> Isinstance (a, int)
False
>>> Isinstance (B, str)
False
>>> Isinstance (B, int) True

• Type (obj)
The type () function returns the data type of an object.

Copy codeThe Code is as follows:
>>> Type ()
<Type 'LIST'>
>>> Type (copy)
<Type 'module'>
>>> Type (1)
<Type 'int'>

Built-in type conversion functions

1. Character and string
• Chr (I)
The chr () function returns the string corresponding to the ASCII code.

Copy codeThe Code is as follows:
>>> Print chr (65)
A
>>> Print chr (66)
B
>>> Print chr (65) + chr (66)
AB

• Ord (x)
The ord () function returns the ASCII or Unicode value of a string parameter.

Copy codeThe Code is as follows:
>>> Ord ("")
97
>>> Ord (u "")
97

• Str (obj)
The str () function converts an object to a printable string.

Copy codeThe Code is as follows:
>>> Str ("4 ")
'4'
>>> Str (4)
'4'
>>> Str (3 + 2j)
'(3 + 2j )'

2. hexadecimal conversion
• Int (x [, base])
The int () function converts numbers and strings into an integer. base is the optional base.

Copy codeThe Code is as follows:
>>> Int (3.3)
3
>>> Int (3L)
3
>>> Int ("13 ")
13
>>> Int ("14", 15)
19

• Long (x [, base])
The long () function converts numbers and strings into integers, and the base is an optional base.

Copy codeThe Code is as follows:
>>> Long ("123 ")
123L
>>> Long (11)
11L

• Float (x)
The float () function converts a number or string to a floating point number.

Copy codeThe Code is as follows:
>>> Float ("12 ")
12.0
>>> Float (12L)
12.0
>>> Float (1, 12.2)
12.199999999999999

• Hex (x)
The hex () function converts an integer to a hexadecimal number.

Copy codeThe Code is as follows:
>>> Hex (16)
'0x10'
>>> Hex (1, 123)
'0x7b'

• Oct (x)
The oct () function converts an integer to an octal number.

Copy codeThe Code is as follows:
>>> Oct (8)
'0'
>>> Oct (123)
'123'

• Complex (real [, imaginary])
The complex () function converts a string or number to a plural number.

Copy codeThe Code is as follows:
>>> Complex ("2 + 1j ")
(2 + 1j)
>>> Complex ("2 ")
(2 + 0j)
>>> Complex (2, 1)
(2 + 1j)
>>> Complex (2L, 1)
(2 + 1j)

3. Data Structure
• Tuple (x)
The tuple () function converts a sequence object to a tuple.

Copy codeThe Code is as follows:
>>> Tuple ("hello world ")
('H', 'E', 'l', 'l', 'O', '', 'w', 'O', 'R', 'l ', 'd ')
>>> Tuple ([1, 2, 3, 4])
(1, 2, 3, 4)

• List (x)
The list () function converts a sequence object to a list. For example:

Copy codeThe Code is as follows:
>>> List ("hello world ")
['H', 'E', 'l', 'l', 'O', '', 'w', 'O', 'R', 'l ', 'D']
>>> List (1, 2, 3, 4 ))
[1, 2, 3, 4]

Sequence processing functions
Len (), max (), and min () in common functions can also be used for sequences.

• Filter (function, list)
When filter () is called, it applies a function to each item in the sequence, and returns all items in the return value of the function to the true value, so as to filter out all items that return the false value.

Copy codeThe Code is as follows:
>>> Def nobad (s ):
... Return s. find ("bad") =-1
...
>>> S = ["bad", "good", "bade", "we"]
>>> Filter (nobad, s)
['Good', 'we']

• Map (function, list [, list])
The map () function applies a function to all items in the sequence and returns a list.

Copy codeThe Code is as follows:
>>> Import string
>>> S = ["python", "zope", "linux"]
>>> Map (string. capitalize, s)
['Python', 'zope ', 'linux']

Map () can also be applied to multiple lists at the same time. For example:

Copy codeThe Code is as follows:
>>> Import operator
>>> S = [1, 2]; t = [3, 2, 1]
>>> Map (operator. mul, s, t) # s [I] * t [j]
[3, 4, 3]

If a None value is passed, instead of a function, map () merges the corresponding elements in each sequence and returns the tuples. For example:

Copy codeThe Code is as follows:
>>> A = [1, 2]; B = [3, 4]; c = [5, 6]
>>> Map (None, a, B, c)
[(1, 3, 5), (2, 4, 6)]

• Reduce (function, seq [, init])
The reduce () function obtains the first two items in the sequence and passes them to the provided function. After obtaining the result, it obtains the next item in the sequence, passes the result to the function, and so on, until all items are processed.

[Code]
>>> Import operator
>>> Reduce (operator. mul, [2, 3, 4, 5]) # (2*3) * 4) * 5
120
>>> Reduce (operator. mul, [2, 3, 4, 5], 1) # (1*2) * 3) * 4) * 5
120
>>> Reduce (operator. mul, [2, 3, 4, 5], 2) # (2*2) * 3) * 4) * 5
240
[Code]

Wklken
Email: wklken@yeah.net


How to view built-in functions in python

Import sys
Print dir (sys. modules ['_ builtin _'])

Help me explain the getattr built-in functions in Python

In fact, the main function of this method is to implement the reflection mechanism. That is to say, you can use strings to obtain the Method Instance. In this way, you can place the methods that a class may call in the configuration file and dynamically load them as needed.

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.