Python basic function 01 (A ~ C)

The Python interpreter has many built-in functions and types that are always available. They are listed here in alphabetical order.

Built-in Functions Built-in Functions
abs() all()
any() ascii()
bin() bool()
breakpoint() bytearray()
bytes() callable()
chr() classmethod()
oct() compile()
complex()

abs(x )

Returns the absolute value of a number. The parameter can be an integer or a floating point number. If the argument is a complex number, its size is returned. If x defines __abs __ (), abs (x) returns x. Abs ().

all(iterable)

Returns True if all element iterations are true (or if iterable is empty). Equivalent to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

any(iterable)

If any element of iterable is true, return True. If iterable is empty, return False. Equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

ascii(object)

Such as repr (), a printable representation of a string containing the object is returned from the returned string, but to escape non-ASCII characters repr () uses \ x, \ u, or \ U to escape. This will generate a string similar to that returned by repr () in Python 2.

am (x)

Convert an integer to a binary string prefixed with "0b". The result is a valid Python expression. If x is not a Python int object, you must define a __index __ () method that returns an integer. Some examples:

bin(3)
'0b11'
bin(-10)
'-0b1010'

If the prefix "0b" is not required, you can use one of the following two methods.

format(14, '#b'), format(14, 'b')
('0b1110', '1110')
f'{14:#b}', f'{14:b}'
('0b1110', '1110')

See also format () for more information.

class bool([ x ] )

Returns a Boolean value, that is True or one of False. x is converted using standard truth test procedures. If x is false or omitted, return False; otherwise return True. The bool class is a subclass of int (see numeric types-integer, floating point, compound). It cannot be further subdivided. The only examples of it are False and True (see Boolean).

Changed in version 3.7: x is now a position-only parameter.

breakpoint(* args,** kws )

This feature allows you to enter the debugger of the call site. Specifically, it calls sys.breakpointhook (), passing args and kws directly. By default, sys.breakpointhook () without any parameters calls pdb.set_trace (). In this case, it is purely a convenience feature, so you do not have to explicitly import pdb or type as much code as possible to enter the debugger. However, sys.breakpointhook () can set it to another function, and breakpoint () will automatically call the function, allowing you to enter the debugger of your choice.

Use parameters to raise audit events. builtins.breakpointbreakpointhook

New features in version 3.7.

class bytearray([source[, encoding[, errors]]])

Return a new byte array. The bytearray class is a variable sequence of integers ranging from 0 to a variable sequence <= X <256 which has most of the conventional methods of variable sequences, as described in the variable sequence types, and most methods that the bytes type has, See byte and ByteArray operations.

The optional source parameter can be used to initialize the array in several different ways:

If it is a string, you must also provide the encoding (and optional errors) parameter; bytearray () then uses to convert the string to bytes str.encode ().

If it is an integer, the array will have that size and will be initialized with null bytes.

If it is an object that conforms to the buffer interface, the read-only buffer of the object will be used to initialize the bytes array.

If it is iterable, it must be an iterable of integers in the range, which are used as the initial contents of the array. 0 <= x <256

Without parameters, an array of size 0 will be created.

See also binary sequence types-bytes, byte arrays, memory views, and byte array objects.

class bytes([source[, encoding[, errors]]])

Returns a new "byte" object, which is an immutable sequence of integers in the range. Yes invariant version-it has the same non-mutation method and the same indexing and slicing behavior. 0 <= x <256bytesbytearray

Therefore, interpret the constructor parameters as bytearray ().

Byte objects can also be created using text, see String and Bytes text.

See also binary sequence types—bytes, byte arrays, memory views, byte objects, and byte and byte array operations.

callable(object)

True returns if the object parameter looks callable, False, otherwise. If it returns True, the call may still fail, but if it returns, the False call object will never succeed. Note that the class is callable (calling a class will return a new instance). If the class of the instance has the __call __ () method, they are callable.

New features in version 3.2: This feature was first removed in Python 3.0 and then reused in Python 3.2.

chr(i)

Returns a string representing a character whose Unicode code point is the integer i. For example, chr (97) returns the string 'a', while chr (8364) returns the string '€'. This is the inverse function of ord ().

The valid range of the parameter is from 0 to 1,114,111 (0x10FFFF to 16). ValueError will be raised if I am not in this range.

@classmethod

Convert methods to class methods.
Class methods receive the class as the implicit first parameter, just as instance methods receive instances. To declare class methods, use the following idiom:

class C:
    @classmethod
    def f(cls, arg1, arg2, ...): ...

The @classmethod form is a function decorator-see function definition for details.

Class methods can be called on a class (such as Cf ()) or an instance (such as C (). F ()). This instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implicit first parameter.

Class methods are different from C ++ or Java static methods. If you need these, see staticmethod ().

For more information about class methods, see Standard Type Hierarchy.

compile(source,filename,mode,flags = 0,dont_inherit = False,optimize = -1 )

Compile the source into code or AST objects. The code object can execute eval () via exec () or. The source can be an ordinary string, a byte string or an AST object. ast For information on how to use AST objects, please refer to the module documentation.

The file name parameter should be given to the file read from the code; if it is not read from the file ('' usually used), then pass some recognizable value.

The mode parameter specifies what kind of code must be compiled; it can be 'exec' if the source contains a sequence of statements, 'eval' if it is expressed by a single, or 'single' if it is expressed by a single interactive statement (In the latter case, the expression statement None will be printed other than something else).

Optional parameter flags and dont_inherit control which future statements will affect source compilation. If neither exists (or neither is zero), the code compile () is compiled using future statements that are valid in the calling code. If the flags parameter is given and dont_inherit is not (or zero), then in addition to those statements that will always be used, future statements specified by the flags parameter will also be used. If dont_inherit is a non-zero integer, the flags parameter is used – ignore future statements that are valid around the compilation call.

Future statements are specified by bits, and these bits can be ORed bit by bit to specify multiple statements. The bit field required to specify a given function can be found as an attribute __future__ on the _Feature instance of compiler_flag in the module.

The optional parameter flag also controls whether the compilation source is allowed to contain top-level await, and. After this bit is set, the return code object has been set and can be executed interactively. async forasync withast.PyCF_ALLOW_TOP_LEVEL_AWAITCO_COROUTINEco_codeawait eval (code_object)

The parameter optimize specifies the optimization level of the compiler; the default value of -1 selects the optimization level of the interpreter given by -Ooptions. The explicit level is 0 (no optimization; __debug__ is true), 1 (assert that __debug__ is false, false) or 2 (document string is also removed).

SyntaxError If the compiled source is invalid and the ValueError source contains null bytes, this function raises.

If you want to parse Python code into AST representation, see ast.parse ().

Use parameters and trigger audit events. Implicit compilation may also raise this event. compilesourcefilename

Note that when compiling a string with multiple lines of code in 'single' or 'eval' mode, the input must be terminated with at least one newline character. This is to facilitate the detection of incomplete and complete statements in the code module.

Warning Due to stack depth limitations in the Python AST compiler, when compiling to an AST object, using a sufficiently large / complex string may crash the Python interpreter.
Changed in version 3.2: Windows and Mac line breaks are allowed. Similarly, input in 'exec' mode does not have to end with a newline character. Added optimize parameter.

Changed in version 3.5: Previously TypeError was raised when a null byte was encountered in the source.

In the new version 3.8: ast.PyCF_ALLOW_TOP_LEVEL_AWAIT can now be passed in the flag to enable top-level support await, and. async forasync with

class complex([real[, imag]])

The return value is real + imag * 1j complex number, or convert a string or number to a complex number. If the first parameter is a string, it will be interpreted as a complex number and the function must be called without the second parameter. The second parameter cannot be a string. Each parameter can be of any numeric type (including complex numbers). If IMAG is omitted, it defaults to zero, and the structure is used for int and float such as digital conversion. If both parameters are omitted, 0j is returned.

For the general Python object x, please complex (x) delegate x. Complex (). If __complex __ () is not defined, return to __float __ (). If __float __ () is undefined, then return to __index __ ().

Note that when converting from a string, the string must not contain spaces around the center + or-operator. For example, complex ('1 + 2j') is good, but improved. complex ('1 + 2j') ValueError

Complex types are described in numeric types-int, float, complex.

Changed in version 3.6: Allow numbers to be grouped by underscores in code text.

Changes were made in version 3.8: return to __index __ () if complex () and float () undefined.

Published 36 original articles · praised 0 · visits 618

Guess you like

Origin blog.csdn.net/Corollary/article/details/105424860