Home > Backend Development > Python Tutorial > Detailed explanation of python's built-in functions

Detailed explanation of python's built-in functions

巴扎黑
Release: 2017-04-01 13:39:32
Original
1323 people have browsed it

python built-in functions I have been learning python recently. I saw and learned some classifications of python built-in functions and the functions of built-in functions on the Internet. The following are Some lists of built-in functions in python. For beginners’ understanding, the classification may not be accurate. Let’s share some information.
1. Mathematical operations

##range( [start], stop[, step]) Generate a sequence, starting from 0 by default round(x[, n]) Roundingsum(iterable[, start]) Sum the setoct(x)Convert a number to octalhex(x )Convert integer x to hexadecimal stringchr(i)Return integer The ASCII character corresponding to ibin(x)Convert the integer x to a binary stringbool([x])Convert x to Boolean type
abs(x)
Find the absolute value
1. The parameter can be an integer or a complex number
2. If the parameter is a complex number, the modulus of the complex number is returned
complex([real[, imag]])
Create a complex number
pmod(a, b)
Get the quotient and remainder respectively
Note: Both integer and floating point types can be
float([x])
Convert a string or number to a floating point number . If there are no parameters, it will return 0.0
int([x[, base]])
Convert a character to int type, base represents Base
long([x[, base]])
Convert a character to long type
pow(x, y[, z])
Returns the y power of x
















2. Collection class operations

##basestring() cannot be called directly, but can be used as isinstance Judgmentformat(value [, format_spec]) The formatting 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]) min(iterable[, args...][key])dict([arg])list([iterable]) set()frozenset([iterable])str([object]) ##sorted(iterable[, cmp[, key[, reverse]]]) tuple([iterable]) xrange([start], stop[, step]) 3. Logical judgment

Super class of str and unicode



Format output string



Returns the unicode of the given int type


Returns an enumerable object, and the next() method of the object will return A tuple


generates an iterator of an object, and the second parameter represents the delimiter


Returns the maximum value in the set


Returns the minimum value in the set


Create data dictionary


will Converting a collection class to another collection class


set object instantiation


Generate an immutable set


Convert to string type

Team set sorting

Generate a tuple type

The xrange() function is similar to range(), but xrnage() does not create a list, but returns an xrange object, which The behavior is similar to the list, but the list value is only calculated when needed. When the list is large, this feature can save us memory

##all(iterable)1. It is true when all the elements in the set are true1. One of the elements in the set is It is true when it is true If xy, return a positive number
2. In particular, if it is an empty string, it returns True


any(iterable)
2. In particular, if it is an empty string, it returns False


cmp(x, y)


4. Reflection

##hasattr(object, name)Determine whether the object object contains an attribute named namehash(object)If the object object is a hash table type, return the hash value of the object objectid(object)Returns the unique identifier of the object##isinstance(object, classinfo)##issubclass(class, classinfo)len(s) locals() Traverse each element and execute function OperationReturns an object of memory mirror typeSimilar to iterator.next()Base classPackaging of property access Class, after setting, you can access the setter and getter through c.x=value, etc. Merge Operation, starting with the first two parameters, then the results of the first two are combined with the third one for processing, and so onReload moduleSet attribute valueChange an object into a printable formatDeclaring a static method is an annotationReference the parent classReturn the type of the objectReturn the variables of the object, if No parameters are similar to the dict() method##bytearray([source [, encoding [, errors]]]])Returns a byte array 1. If source is an integer, return an initialization array with the length of source; 2. If source is a string, convert the string into a byte sequence according to the specified encoding; I really didn’t understand it, I just saw the changing aspects of the matrix

5. IO operations

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
classmethod()
1. Annotation is used to indicate that this method is a class method
2. A class method can be called by a class or an instance
3. A class method is similar to the static method in Java
4. The self parameter is not required in the class method
compile(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().
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.
dir([object])
1. No When parameters are taken, a list of variables, methods and defined types in the current scope is returned;
2. When parameters are taken, a list of attributes and methods of the parameters is returned.
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
delattr(object, name)
Delete the attribute named name of the object object
eval(expression [, globals [, locals]])
Calculate expression The value of expression
execfile(filename [, globals [, locals]])
The usage is similar to exec(), but the difference is that of execfile The parameter filename 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)]
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()
Return a A dictionary describing the current global symbol table







Judge whether object is an instance of class

Judge whether it is a subclass

Return the collection length

Return the current variable list

##map(function, iterable, ...)


memoryview(obj)


next(iterator[, default])


object()

##property([fget[, fset[, fdel[, doc]]]])

reduce(function, iterable[, initializer])

reload(module)

setattr(object, name, value)

repr(object)

slice ()

staticmethod

super(type[, object-or-type])

type(object)

vars([object])


3. If the source is an iterable type, the elements must be integers in [0, 255];
4. If the source is an object consistent with the buffer interface, this object can also be used to initialize bytearray.



zip([iterable, ...])


##printprint functionraw_input([prompt] ) Set input, input is processed as a string
file(filename [, mode [, bufsize]])
file The constructor of the type is used to open a file. If 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
1. Parameter filename: file name.
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.
input([prompt])
Get user input
It is recommended to use raw_input because this function will not capture user errors Enter
open(name[, mode[, buffering]])
Open the file
What is the difference from file? It is recommended to use open




6. Othershelp() --Help information apply(), buffer(), coerce(), intern()---These are expired built-in functions, so they are not explained7. PostscriptBuilt-in functions are generally due to frequency of use It is relatively frequent or is a meta-operation, so it is provided in the form of built-in functions. Through the classification analysis of Python's built-in functions, we can see that the basic data operations are basically some mathematical operations (except addition, subtraction, multiplication and division, of course), logical operations, Collection operations, basic IO operations, and then the reflection operations on the language itself, and string operations are also commonly used. In particular, reflection operations need to be noted.

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

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