Python built-in functions 1

Source: Internet
Author: User
Tags chr mul ord

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.
>>>print ABS (-100) 100

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

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 Object Package
Includes functions, methods, code objects, classes, and class instances that have already defined the calling method. “ ”
>>> a= "123"

>>> Print callable (a) 0

>>> Print Callable (CHR) 1


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

>>>b=2

>>>c=2

>>> Print CMP (A, B)-1

>>> Print CMP (b,a) 1

>>> Print cmp (b,c) 0


Divmod (x, y)
The Divmod (x, y) function completes the division operation, returning the quotient and remainder.
>>> Divmod (10,3) (3, 1)

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

Isinstance (object,class-or-type-or-tuple), BOOL
Test object Type
>>> a= ' isinstance test '

>>> b=1234

>>> isinstance (A,STR) True

>>>isinstance (A,int) False

>>> isinstance (B,STR) False

>>> isinstance (b,int) True

Len (object), Integer

The Len () function returns the string and the length of the sequence.
>>> len ("AA") 2

>>> Len ([2])

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 Y power value of x as the z-modulo
Value.
>>> Print POW (2,4) 16

>>> Print pow (2,4,2) 0

>>> Print pow (2.4,3) 13.824

Range ([Lower,]stop[,step])
The range () function generates a sequential list of ordered integers by parameter.
>>> 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]

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.
>>> Round (3.333) 3.0

>>> Round (3) 3.0

>>> Round (5.9) 6.0

Type (obj)
The type () function returns the data type of the object.
>>> type (a) <type ' list ' >

>>> type (copy) <type ' module ' >

>>> type (1) <type ' int ' >

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.
>>> A=xrange (10)

>>> Print A[0] 0

>>> Print A[1] 1

>>> Print A[2] 2
Chapter 2. Built-in type conversion functions

Chr (i)
The CHR () function returns a string corresponding to the ASCII code.
>>> print Chr (A)

>>> print chr (+) B

>>> print Chr (+CHR) AB

Complex (Real[,imaginary])
The complex () function converts a string or number to a plural.
>>> Complex ("2+1j") (2+1j)

>>> Complex ("2") (2+0J)

>>> Complex (2,1) (2+1j)
>>> Complex (2l,1) (2+1j)

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

>>> Float (12L) 12.0

>>> Float (12.2) 12.199999999999999

Hex (x)
The hex () function converts integers to hexadecimal numbers.
>>> Hex (+) ' 0x10 '

>>> Hex (123) ' 0x7b '

Long (X[,base])
The long () function converts numbers and strings to grow integers, and base is an optional cardinality.
>>> Long ("123") 123L

>>> Long (one) 11L

List (x)
The list () function converts a sequence object to a list. Such as:
>>> list ("Hello World") [' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']

>>> list ((1,2,3,4)) [1, 2,3, 4]

Int (X[,base])
the int () function converts numbers and strings into an integer, base is an optional cardinality.
>>> Int (3.3) 3

>>> Int (3L) 3

>>> Int ("13") 13

>>> Int ("14", 15) 19

Min (X[,y,z ...])
The min () function returns the minimum value of the given parameter, which can be a sequence.
>>> 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.
>>> Max (1,2,3,4) 4

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

Oct (x)
The OCT () function converts an integer given to an octal number.
>>> Oct (8) ' 010 '

>>> Oct (123) ' 0173 '

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

>>> Ord (U "a") 97

STR (obj)
The STR () function converts an object into a printable string.
>>> Str ("4") ' 4 '

>>> Str (4) ' 4 '

>>> str (3+2j) ' (3+2j) '

Tuple (x)
The tuple () function converts a sequence object to a tuple.
>>> tuple ("Hello World") (' h ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ')

>>> tuple ([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, thus
Filters out all items that return false values.
>>> def Nobad (s):

... return s.find ("bad") = =-1

...

>>> s = ["Bad", "good", "bade", "we"]

>>> filter (nobad,s) [' Good ', ' we ']

This example filters out all items that contain "bad" by applying the Nobad () function to all the items in the S sequence.

Map (Function,list[,list])
The map () function applies a function to all items in the sequence and returns a list.
>>> 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:
>>> 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
The tuple. Such as:
>>> 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 takes the next item in the sequence.
The result is passed to the function, and so on until all items have been processed.
>>> Import operator

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

/em> >>> < EM id= "__mcedel" >reduce (operator.mul,[2,3,4,5],1) # ((1*2) * *)

< EM id= "__mcedel" > > >> reduce (Operator.mul, [2,3,4,5],2) # (((2*2) * * *) ((()) ((())

Zip (seq[,seq,...])
The zip () function merges the corresponding items in two or more sequences and returns them in the form of tuples, after processing the shortest order
Stops after all the items in the column.
>>> zip ([1,2,3],[4,5],[7,8,9]) [(1, 4, 7), (2, 5, 8)]
If the argument is a sequence, zip () returns each item in a tuple format, such as:
>>> Zip ((1,2,3,4,5)) [(1,), (2,), (3,), (4,), (5,)]

>>> zip ([1,2,3,4,5]) [(1,), (2,), (3,), (4,),(5,)]

String Module

Replace (String,old,new[,maxsplit])
The substitution function of the string, replacing the old in the string with new. The default is to replace all old values in the string with the new
Value, if the Maxsplit value is given, the number of replacements can also be controlled, and if Maxsplit is 1, only the first old value is replaced.
>>>a= "11223344"

>>>print String.Replace (A, "1", "one") oneone2223344

>>>print
String.Replace (A, "1", "one", 1) one12223344

Capitalize (String)
This function replaces the first character of a string with a large font.
>>> Import String

>>> print string.capitalize ("python") python

Split (String,sep=none,maxsplit=-1)
Returns a list from a string string, with the value of Sep as the delimiter.
>>> Import String

>>> ip= "192.168.3.3"

>>> ip_list=string.split (IP, '. ')

>>> Print
ip_list [' 192 ', ' 168 ', ' 3 ', ' 3 ']

Python built-in functions 1

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.