Python common functions

Source: Internet
Author: User
Tags chr cmath
BOOL (obj)
Returns the Boolean value of the Obj object, which is the return value of the obj.__nonzero__ () method
int (obj, base=10)
Returns an integer representation of a string or numeric object, similar to String.atoi (); From Python 1.6, an optional input parameter is introduced.
Long (obj, base=10)
Returns a character or long integer representation of a data object, similar to String.atol (), with optional input parameters introduced from Python1.6
Float (obj)
Returns a floating-point representation of a string or data object, similar to String.atof ()
Complex (str), complex (real, imag=0.0)
Returns the plural representation of a string, or generates a complex object based on a given real number (and an optional imaginary part).


ABS (NUM)
Returns the absolute value of num
Coerce (NUM1, num2)
Convert NUM1 and num2 to the same type, and then return as a tuple
Divmod (NUM1, num2)
Division-the combination of the take-rest operation. Returns a tuple (Num1/num2,num1%num2). Rounding of the quotient of floating-point and complex numbers (quotient of complex numbers taking only real parts)
Pow (NUM1, num2, mod=1)
Take the num2 of the NUM1, if the MoD parameter is provided, then the result of the calculation of the MoD to take the remainder operation
Round (FLT, ndig=0)
Accepts a floating-point number flt and rounds it, saving ndig decimal places. If the Ndig parameter is not provided, the default is 0 digits after the decimal point.


Hex (NUM)
Converts a number to a hexadecimal number and returns as a string
Oct (num)
Converts a number to an octal number and returns as a string
Chr (NUM)
Converts a number of ASCII values to ASCII characters with a range of only 0
Ord (CHR)
Accepts an ASCII or Unicode character (a string of length 1), returning the corresponding ASCII or Unicode value.
UNICHR (num)

Accepts Unicode code values and returns their corresponding Unicode characters. The range of code values accepted depends on whether your Python is built on ucs‐2 or ucs‐4.



4. Number Type related modules

Decimal
Decimal floating-point arithmetic class decimal
Array
Efficient array of values (characters, integers, floating-point numbers, etc.)
Math/cmath
Standard C library mathematical operation function. General mathematical operations in the match module, complex operations in the Cmath module
operator
The function implementation of the numeric operator. For example, Tor.sub (M,n) is equivalent to M-n
Random
Multiple pseudo-random number generators


5. Sequence type conversion factory functions

List (ITER)
Convert an iterative object to a list
STR (obj)
Converts the Obj object to a string (the string representation of the object)
Unicode (obj)
Converts an object to a Unicode string (using the default encoding)
Basestring ()
Abstract factory functions, which function only to provide a parent class for STR and Unicode functions, so they cannot be instantiated or called
Tuple (ITER)
Convert an iterative object to a tuple object


6. Built-in functions available for sequence types

Enumerate (ITER)
Takes an iterative object as a parameter, returning a enumerate object (also an iterator) that generates a tuple of index and item values for each element of ITER (PEP 279)
Len (seq)
Returns the length of the SEQ
Max (Iter,key=none) or Max (Arg0,arg1...,key=none)
Return to ITER or (Arg0,arg1,...) The maximum value in the, if key is specified, the key must be a callback function that can be passed to the sort () method for comparison.
Min (iter, Key=none) ormin (arg0, arg1 .... key=none)
Return the minimum value inside ITER; or return (Arg0,arg2,...) The minimum value in the inside; If you specify a key, the key must be a callback function that can be passed to the sort () method for comparison.
Reversed (SEQ)
Takes a sequence as a parameter and returns an iterator that is accessed in reverse order (PEP 322)
Sorted (Iter,func=none,key=none,reverse=false)
Takes an iterative object as an argument, returns an ordered list, and the optional parameters Func,key and reverse have the same meaning as the parameters of the List.sort () built-in function.
SUM (seq, init=0)
Returns the sum of the SEQ and optional parameter init, which has the same effect as reduce (operator.add,seq,init)
Zip ([it0, It1,... ItN])
Returns a list of the first elements of these elements that comprise a tuple. For example: Zip ([' A ', ' 1 ', ' B ', ' d ', ' e ']) >>> [(' A ',), (' 1 ',), (' B ',), (' d ',), (' E ',)]


7. String Formatting symbols

%c
Convert to character (ASCII value, or string of one length)
%r
Prioritize string conversions with the repr () function
%s
Prioritize string conversions with the STR () function
%d/%i
Turn into a signed decimal number
%u
Turn into unsigned decimal numbers
%o
Turn unsigned octal number
%x/%x
(Unsigned) turns to unsigned hexadecimal number (x/x represents the case of the converted hexadecimal character)
%e/%e
Turn into scientific notation (e/e control output e/e
%f/%f
Turn into floating point number (fractional part natural truncation
%g/%g
Shorthand for%e and%f/%e and%f
%%
Output

8. Formatting operator Auxiliary Instructions

*
Define width or decimal precision
-
Used for left justification
+
Show plus sign (+) in front of positive number
<sp>
Show spaces in front of positive numbers
#
Displays 0 (' 0 ') before the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on
Using ' x ' or ' x ')
0
The displayed number is preceded by ' 0 ' instead of the default space
%
' percent ' output a single '% '
(VAR)
mapping variables (dictionary parameters)
M.n
M is the minimum total width displayed, and n is the number of digits after the decimal point (if available)
  • 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.