Home > Backend Development > Python Tutorial > Detailed introduction to python built-in functions

Detailed introduction to python built-in functions

高洛峰
Release: 2017-03-21 10:41:25
Original
1673 people have browsed it

To summarize the built-in functions, Build-in Function.

1. Mathematical operations

##oct(x)Convert a number to octalhex(x)Convert the integer x to a hexadecimal string chr(i)Return the ASCII corresponding to the integer i Charactersbin(x)Converts the integer x to a binary stringbool([x])Convert x to Boolean type
abs(x)

Find the absolute value

complex([real[, imag]]) Create a complex number
pmod(a, b) Get the quotient and remainder respectively
Note: Both integer and floating point types can
float([x]) Convert a string or number to Floating point number. If there are no parameters, it will return 0.0
int([x[, base]]) Convert a character to int type, base represents the base number
long([x[, base]]) Convert a character to long type
pow(x, y[, z]) Returns the y power of x
range([start], stop[, step]) Generates a sequence, default Starting from 0
round(x[, n]) Rounding
sum(iterable[, start]) Sum the set

2. Collection class operations

##basestring() cannot be called directly, but can be used as isinstance judgmentformat(value [, format_spec])The formatted parameter sequence starts from 0, such as "I am {0},I like {1}"unichr( i)enumerate(sequence [, start = 0])iter(o[, sentinel])##max(iterable[, args...][key]) Returns the maximum value in the setmin(iterable[, args...][key])Returns the minimum value in the setdict([arg])Create data dictionarylist([iterable]) Convert one collection class to another collection classset()set object instantiationfrozenset([iterable])Produces an immutable setstr([object]) Convert to string typesorted(iterable[, cmp[, key[, reverse]]]) Team collection sortingGenerate a tuple typeThe xrange() function is similar to range(), but xrnage() does not create a list, but returns an xrange object, which behaves like a list. But only calculate list values ​​when needed. This feature can save us memory when the list is large
Super class of str and unicode
Formatted output string
Returns unicode of the given int type
Returns an enumerable object , the next() method of the object will return a tuple
generates an iterator of the object, the second parameter represents Delimiter
##tuple([iterable])
xrange ([start], stop[, step])

3. Logical judgment

all(iterable) 1. When all the elements in the set are true, it is True
2. In particular, if it is an empty string, it returns True
any(iterable) 1. When one element in the set is true Is true
2. In particular, if it is an empty string, it returns False
cmp(x, y) If x < y, a negative number is returned; x == y, returns 0; x > y, returns a positive number

4. Reflection

##classmethod()1. Annotation is used to indicate that this method is a class methodcompile(source, filename, mode[, flags[, dont_inherit]])Compile source into code or AST object. Code objects can be executed via the exec statement or evaluated with eval(). dir([object])1. Without parameters, return the current List of variables, methods and defined types within the scope; delattr(object, name)Delete the object object Attribute named nameeval(expression [, globals [, locals]])Calculate the value of expression expressionexecfile(filename [, globals [, locals]])The usage is similar to exec(), except that the parameter filename of execfile is the file name, and the parameter of exec is a string. filter(function, iterable)Construct a sequence, which is equivalent to [item for item in iterable if function(item)]##len( s) Return the collection lengthlocals() Return the current variable listmap (function, iterable, ...) Traverse each element and perform the function operationmemoryview(obj) Return a memory image type The object of next(iterator[, default]) is similar to iterator.next()##object() property([fget[, fset[, fdel[, doc]]]]) reduce(function, iterable[, initializer]) reload(module) setattr(object, name, value)repr(object) slice()staticmethodsuper(type[, object-or-type]) type (object)vars([object]) bytearray([source [, encoding [, errors]]])1. If source is an integer, return An initialization array with a length of source; zip([iterable , ...])
callable(object) Check whether the object object is callable
1. The class can be called
2. The instance cannot be called Unless the __call__ method is declared in the class
2. Class Methods can be called by classes or instances
3. Class methods are similar to static methods in Java
4. There is no self parameter required in class methods
1. Parameter source: string or AST (Abstract Syntax Trees) object.
2. Parameter filename: the name of the code file. If the code is not read from the file, some identifiable values ​​will be passed.
3. Parameter model: Specify the type of compiled code. Can be specified as 'exec', 'eval', 'single'.
4. Parameters flag and dont_inherit: These two parameters will not be introduced for the time being.
2. When taking parameters, return the list of properties and methods of the parameters.
3. If the parameter contains the method __dir__(), this method will be called. When the parameter is an instance.
4. If the parameter does not contain __dir__(), this method will collect parameter information to the maximum extent
1. Parameter function : A function whose return value is True or False, which can be None
2. Parameter iterable: sequence or iterable object
getattr(object, name [, defalut]) Get the attributes of a class
globals() Returns a dictionary describing the current global symbol table
hasattr(object, name) Judge object object Whether to include the attribute named name
hash(object) If the object object is a hash table type, return the hash value of the object object
id(object) Returns the unique identifier of the object (memory identifier) ​​
isinstance(object, classinfo) Determine whether object is an instance of class
issubclass(class, classinfo) Determine whether it is a subclass
Base class
Wrapper class for property access, After setting, you can access the setter and getter through c.x=value, etc.
Merge operation, starting from the first The first two parameters, then the results of the first two are combined with the third one for processing, and so on
Reload module
Set attribute value
will An object is transformed into a printable format
 
Declare static Method is an annotation
references the parent class
Returns the type of the object
Returns the object's variables, if there are no parameters and dict() The method is similar to
Returns a byte array2. If source is a string, convert the string into a byte sequence according to the specified encoding;
3. If source is an iterable type, the element must be [ 0,255];
4. If the source is an object consistent with the buffer interface, this object can also be used to initialize bytearray.

is approximately equal to a zipper, which is to arrange the elements in the two lists one by one

5. IO operation

file(filename [, mode [, bufsize]])1. Parameter filename: file name. input([prompt]) It is recommended to use raw_input, because this function will not capture user input errorsopen(name[, mode[, buffering]]) What is the difference from file? It is recommended to use openprintraw_input([prompt])
Constructor of file type, which is used to open a file. If When the file does not exist and the mode is write or append, the file will be created. Adding 'b' to the mode parameter will operate on the file in binary form. Adding '+' to the mode parameter will allow simultaneous read and write operations on the file 2. Parameter mode: 'r' (read), 'w' (write), 'a' (append).
3. Parameter bufsize: If it is 0, it means no buffering. If it is 1, it means line buffering. If it is a number greater than 1, it means the size of the buffer.

Get user input
Open a file
Print function
Set input , the input is processed as a string

The above is the detailed content of Detailed introduction to python built-in functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template