Python Introductory and advanced notes Python built-in function summary

Source: Internet
Author: User
Tags mul
Built-in functions
Common functions

1. Math-related
ABS (x)
ABS () returns the absolute value of a number. If the plural is given, the return value is the modulus of the complex number.

Copy the Code code as follows:


>>>print ABS (-100)
100
>>>print ABS (1+2J)
2.2360679775

divmod (x, y)
The Divmod (x, y) function completes the division operation, returning the quotient and remainder.

Copy the Code code as follows:


>>> Divmod (10,3)
(3, 1)
>>> Divmod (9,3) (3, 0)

Pow (x,y[,z])
The POW () function returns a power of x, and y as the exponent. If the z-value is given, the function calculates the value of the Y-power of x being the z-modulo.

Copy the Code code 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 the floating-point number x, if given an n value, that represents the digits rounded to the decimal point.

Copy the Code code 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 the given parameter, which can be a sequence.

Copy the Code code as follows:


>>> min (1,2,3,4)
1
>>> min ((2,3,4))
(1, 2, 3)

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

Copy the Code code as follows:


>>> Max (1,2,3,4)
4
>>> Max ((+), (2,3,4))
(2, 3, 4)

2. Sequence Correlation

Len (object), Integer
The Len () function returns the string and the length of the sequence.

Copy the Code code as follows:


>>> len ("AA")
2
>>> Len ([up])
2

range ([Lower,]stop[,step])
The range () function generates a sequential list of ordered integers by parameter.

Copy the Code code 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 () does not create a list, but instead returns a Xrange object whose behavior

Similar to a list, but only when needed to calculate the value of the list, when the list is large, this feature can save US memory.

Copy the Code code 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 an object can be called, or 1 (true) if it is possible, otherwise 0 (false) is returned. Callable objects include functions, methods, code objects, classes, and class instances that have defined the calling method.

Copy the Code code as follows:


>>> a= "123"
>>> Print callable (a)
False
>>> Print callable (CHR)
True

cmp (x, y)
The CMP () function compares x and y two objects and returns an integer based on the result of the comparison, if x y, returns 1 if X==y returns 0.

Copy the Code code as follows:


>>>a=1
>>>b=2
>>>c=2
>>> Print CMP (A, B)
-1
>>> Print CMP (b,a)
1
>>> Print CMP (B,C)
0

isinstance (object,class-or-type-or-tuple), BOOL
Test object Type

Copy the Code code 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 the object.

Copy the Code code as follows:


>>> type (a)

>>> type (copy)

>>> Type (1)

Built-in type conversion functions

1. Characters and strings
chr (i)
The CHR () function returns a string corresponding to the ASCII code.

Copy the Code code as follows:


>>> Print Chr (65)
A
>>> Print Chr (66)
B
>>> print Chr (+CHR) (66)
Ab

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

Copy the Code code as follows:


>>> Ord ("a")
97
>>> Ord (U "a")
97

Str (obj)
The STR () function converts an object into a printable string.

Copy the Code code as follows:


>>> Str ("4")
' 4 '
>>> Str (4)
' 4 '
>>> Str (3+2J)
' (3+2j) '

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

Copy the Code code 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 to grow integers, and base is an optional cardinality.

Copy the Code code as follows:


>>> Long ("123")
123L
>>> Long (11)
11L

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

Copy the Code code as follows:


>>> Float ("12")
12.0
>>> Float (12L)
12.0
>>> Float (12.2)
12.199999999999999

Hex (x)
The hex () function converts integers to hexadecimal numbers.

Copy the Code code as follows:


>>> Hex (16)
' 0x10 '
>>> Hex (123)
' 0x7b '

Oct (x)
The OCT () function converts an integer given to an octal number.

Copy the Code code as follows:


>>> Oct (8)
' 010 '
>>> Oct (123)
' 0173 '

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

Copy the Code code 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 the Code code 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. Such as:

Copy the Code code 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 are also used for sequences.

Filter (function,list)
When you call filter (), it applies a function to each item in the sequence and returns all items when the function returns True, thereby filtering out all items that return false values.

Copy the Code code 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 the Code code 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. Such as:

Copy the Code code as follows:


>>> Import operator
>>> s=[1,2,3]; t=[3,2,1]
>>> map (operator.mul,s,t) # S[i]*t[j]
[3, 4, 3]

If you pass a none value instead of a function, map () merges the corresponding elements in each sequence and returns that tuple. Such as:

Copy the Code code 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, passes it to the provided function, obtains the result, and then passes the next item in the sequence, along with the result, to the function, and so on, until all items have been processed.

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

Wklken
Email:wklken@yeah.net

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