Built-in functions — Python 3.16.0a2 documentation
Navigation index modules | next | previous | Python » 3.16.0a2 documentation » Python built-ins reference » Built-in functions | Theme | Built-in functions ¶ The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order. Built-in Functions A abs() aiter() all() anext() any() ascii() B bin() bool() breakpoint() bytearray() bytes() C callable() chr() classmethod() compile() complex() D delattr() dict() dir() divmod() E enumerate() eval() exec() F filter() float() format() frozendict() frozenset() G getattr() globals() H hasattr() hash() help() hex() I id() input() int() isinstance() issubclass() iter() L len() list() locals() M map() max() memoryview() min() N next() O object() oct() open() ord() P pow() print() property() R range() repr() reversed() round() S set() setattr() sentinel() slice() sorted() staticmethod() str() sum() super() T tuple() type() V vars() Z zip() _ __import__() abs ( number , / ) ¶ Return the absolute value of a number.
The argument may be an integer, a floating-point number, or an object implementing __abs__() . If the argument is a complex number, its magnitude is returned. aiter ( async_iterable , / ) ¶ aiter ( callable , / , stop_value , * , stop_exception = StopAsyncIteration ) aiter ( callable , / , * , stop_exception ) Return an asynchronous iterator object. The first argument is interpreted very differently depending on the presence of the other arguments.
Without other arguments, the single argument must be an asynchronous iterable , and the result is equivalent to calling x.__aiter__() . If stop_value or stop_exception is given, then the first argument must be a callable object. The asynchronous iterator created in this case calls callable with no arguments and awaits the result for each call to its __anext__() method; if the awaited value is equal to stop_value , or if the call raises an exception matching stop_exception , StopAsyncIteration will be raised, otherwise the value will be returned.
The callable is only called when the result of __anext__() is awaited. stop_exception is an exception class or a tuple of exception classes. If stop_value is not specified, the iteration stops only when the callable raises an exception. If the callable raises StopAsyncIteration which does not match stop_exception , it is replaced with a RuntimeError , as for asynchronous generators (see PEP 525 ). For example, reading fixed-size chunks from an asynchronous stream until the end of file is reached: from functools import partial async for chunk in aiter ( partial ( reader . read , 1024 ), b '' ): process_chunk ( chunk ) Or consuming an asyncio.Queue until it is shut down: from asyncio import QueueShutDown async for item in aiter ( queue . get , stop_exception = QueueShutDown ): process_item ( item ) Added in version 3.10.
Changed in version 3.16: Added the stop_value and stop_exception parameters. all ( iterable , / ) ¶ Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to: def all ( iterable ): for element in iterable : if not element : return False return True awaitable anext ( async_iterator , / ) ¶ awaitable anext ( async_iterator , default , / ) When awaited, return the next item from the given asynchronous iterator , or default if given and the iterator is exhausted .
This is the async variant of the next() builtin, and behaves similarly. This calls the __anext__() method of async_iterator , returning an awaitable . Awaiting this returns the next value of the iterator. If default is given, it is returned if the iterator is exhausted, otherwise StopAsyncIteration is raised. Added in version 3.10. any ( iterable , / ) ¶ Return True if any element of the iterable is true. If the iterable is empty, return False .
Equivalent to: def any ( iterable ): for element in iterable : if element : return True return False ascii ( object , / ) ¶ As repr() , return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using \x , \u , or \U escapes. This generates a string similar to that returned by repr() in Python 2. bin ( integer , / ) ¶ Convert an integer number to a binary string prefixed with “0b”.
The result is a valid Python expression. If integer is not a Python int object, it has to define an __index__() method that returns an integer. Some examples: >>> bin ( 3 ) '0b11' >>> bin ( - 10 ) '-0b1010' If the prefix “0b” is desired or not, you can use either of the following ways. >>> format ( 14 , '#b' ), format ( 14 , 'b' ) ('0b1110', '1110') >>> f ' { 14 : #b } ' , f ' { 14 : b } ' ('0b1110', '1110') See also enum.bin() to represent negative values as twos-complement.
See also format() for more information. class bool ( object = False , / ) ¶ Return a Boolean value, i.e. one of True or False . The argument is converted using the standard truth testing procedure . If the argument is false or omitted, this returns False ; otherwise, it returns True . The bool class is a subclass of int (see Numeric Types — int, float, complex ). It cannot be subclassed further. Its only instances are False and True (see Boolean Type - bool ).
Changed in version 3.7: The parameter is now positional-only. breakpoint ( * args , ** kws ) ¶ This function drops you into the debugger at the call site. Specifically, it calls sys.breakpointhook() , passing args and kws straight through. By default, sys.breakpointhook() calls pdb.set_trace() expecting no arguments. In this case, it is purely a convenience function so you don’t have to explicitly import pdb or type as much code to enter the debugger.
However, sys.breakpointhook() can be set to some other function and breakpoint() will automatically call that, allowing you to drop into the debugger of choice. If sys.breakpointhook() is not accessible, this function will raise RuntimeError . By default, the behavior of breakpoint() can be changed with the PYTHONBREAKPOINT environment variable. See sys.breakpointhook() for usage details. Note that this is not guaranteed if sys.breakpointhook() has been replaced.
Raises an auditing event builtins.breakpoint with argument breakpointhook . Added in version 3.7. class bytearray ( source = b'' ) class bytearray ( source , encoding , errors = 'strict' ) Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types , as well as most methods that the bytes type has, see Bytes and Bytearray Operations .
The optional source parameter can be used to initialize the array in a few different ways: If it is a string , you must also give the encoding (and optionally, errors ) parameters; bytearray() then converts the string to bytes using 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 conforming to the buffer interface , a read-only buffer of the object will be used to initialize the bytes array.
If it is an iterable , it must be an iterable of integers in the range 0 <= x < 256 , which are used as the initial contents of the array. Without an argument, an array of size 0 is created. See also Binary Sequence Types — bytes, bytearray, memoryview and Bytearray Objects . class bytes ( source = b'' ) class bytes ( source , encoding , errors = 'strict' ) Return a new “bytes” object which is an immutable sequence of integers in the range 0 <= x < 256 . bytes is an immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.
Accordingly, constructor arguments are interpreted as for bytearray() . Bytes objects can also be created with literals, see String and Bytes literals . See also Binary Sequence Types — bytes, bytearray, memoryview , Bytes Objects , and Bytes and Bytearray Operation
The rest of this page is on the live site.