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

Detailed introduction to python built-in functions

Mar 21, 2017 am 10:41 AM
python

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

See all articles