builtins – builtin functions and exceptions

All builtin functions and exceptions are described here. They are also available via builtins module.

Functions and types

abs(x: Any) Any

Return the absolute value of a number. The argument may be an integer, a float, or any object implementing __abs__().

all(iterable: Iterable[Any]) bool

Return True if all elements of iterable are truthy (or if the iterable is empty).

any(iterable: Iterable[Any]) bool

Return True if any element of iterable is truthy. Returns False if the iterable is empty.

bin(x: int) str

Convert an integer to a binary string prefixed with "0b". The argument must be a Python integer or implement __index__().

class bool(x: Any = False)

Return a boolean value, i.e. one of True or False. x is converted using the standard truth testing procedure.

class bytearray(source: int | str | Iterable[int] | bytes = b'', encoding: str = 'utf-8', errors: str = 'strict')

Mutable sequence of integers in the range 0-255. Construction follows the same rules as bytes: from an integer (creating a zero-filled buffer of that size), an iterable of ints, a string with encoding, or any buffer-protocol object. Supports the standard sequence operations plus in-place modification.

class bytes(source: int | str | Iterable[int] = b'', encoding: str = 'utf-8', errors: str = 'strict')

Immutable sequence of integers in the range 0-255. Created from an integer (zero-filled buffer), an iterable of ints, a string with encoding, or any buffer-protocol object. Bytes literals use the b'...' syntax.

callable(obj: Any) bool

Return True if obj appears callable, False otherwise.

chr(i: int) str

Return a string of one character whose Unicode code point is the integer i.

classmethod(func: Callable[..., Any]) classmethod

Transform a method into a class method. Typically used as a decorator.

compile(source: str | bytes, filename: str, mode: str) Any

Compile source into a code object that can be executed by exec() or eval(). mode is one of "exec", "eval" or "single".

class complex(real: float | str = 0, imag: float = 0)

Create a complex number from a real and imaginary part, or from a string.

delattr(obj, name: str) None

The argument name should be a string, and this function deletes the named attribute from the object given by obj.

class dict(*args, **kwargs)

Create a new dictionary. Equivalent to CPython’s dict.

dir(obj: Any = None) list

Without arguments, return the list of names in the current local scope. With an argument, return a list of valid attributes for that object.

divmod(a: Any, b: Any) tuple

Return the pair (a // b, a % b) as a tuple, for two (non-complex) numbers.

enumerate(iterable: Iterable[Any], start: int = 0) Iterator[tuple]

Return an enumerate object yielding (index, value) pairs from iterable, with the index starting at start.

eval(expression: str | bytes, globals: dict | None = None, locals: dict | None = None) Any

Evaluate a Python expression given as a string (or compiled code object) and return the result.

exec(object: str | bytes, globals: dict | None = None, locals: dict | None = None) None

Dynamically execute Python code provided as a string or compiled code object.

filter(function: Callable[[Any], Any] | None, iterable: Iterable[Any]) Iterator[Any]

Construct an iterator from those elements of iterable for which function returns true. If function is None, the identity function is assumed.

class float(x: str | bytes | int | float = 0.0)

Return a floating point number constructed from a number or string x.

class frozenset(iterable: Iterable[Any] = ())

Return a new frozenset object, optionally with elements taken from iterable. frozenset is an immutable, hashable variant of set.

getattr(obj: Any, name: str, default: Any = ...) Any

Return the value of the named attribute of obj. If the attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

globals() dict

Return a dictionary representing the current module’s global symbol table.

hasattr(obj: Any, name: str) bool

Return True if obj has an attribute with the given name, False otherwise.

hash(obj: Any) int

Return the hash value of obj (if it has one). Hash values are integers used to quickly compare dictionary keys during a dictionary lookup.

hex(x: int) str

Convert an integer to a lowercase hexadecimal string prefixed with "0x".

id(obj: Any) int

Return the identity of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime.

input(prompt: str = '') str

Read a line from standard input and return it as a string (without a trailing newline). If prompt is given, it is written to standard output without a trailing newline first.

class int(x: str | bytes | int | float = 0, base: int = 10)
classmethod from_bytes(bytes: bytes, byteorder: str) int

In MicroPython, byteorder parameter must be positional (this is compatible with CPython).

to_bytes(size: int, byteorder: str) bytes

In MicroPython, byteorder parameter must be positional (this is compatible with CPython).

Note

The optional signed kwarg from CPython is not supported. MicroPython currently converts negative integers as signed, and positive as unsigned. (Details.)

isinstance(obj: Any, classinfo: type | tuple) bool

Return True if obj is an instance of classinfo or any of its subclasses. classinfo may be a class or a tuple of classes.

issubclass(cls: type, classinfo: type | tuple) bool

Return True if cls is a subclass (direct, indirect, or virtual) of classinfo.

iter(obj: Any, sentinel: Any = ...) Iterator[Any]

Return an iterator object. With one argument, obj must support the iteration protocol. With two arguments, obj must be callable and iteration stops when it returns sentinel.

len(obj: Any) int

Return the number of items in a container.

class list(iterable: Iterable[Any] = ())

Build a new list, optionally populated from items in iterable.

locals() dict

Return a dictionary representing the current local symbol table.

map(function: Callable[..., Any], *iterables: Iterable[Any]) Iterator[Any]

Return an iterator that applies function to every item of iterables, yielding the results.

max(*args: Any, key: Callable[[Any], Any] | None = None, default: Any = ...) Any

With a single iterable argument, return its largest item. With two or more arguments, return the largest argument.

class memoryview(obj: Any)

Create a memoryview that references obj, which must support the buffer protocol (e.g. bytes, bytearray, array.array). Allows zero-copy access and slicing of the underlying memory; slicing a memoryview returns another memoryview rather than a copy.

min(*args: Any, key: Callable[[Any], Any] | None = None, default: Any = ...) Any

With a single iterable argument, return its smallest item. With two or more arguments, return the smallest argument.

next(iterator: Iterator[Any], default: Any = ...) Any

Retrieve the next item from iterator. If default is given and the iterator is exhausted, default is returned instead of raising StopIteration.

class object

Return a new featureless object. object is the base class for all classes.

oct(x: int) str

Convert an integer to an octal string prefixed with "0o".

open(file: str, mode: str = 'r', **kwargs) Any

Open file and return a corresponding file object. mode defaults to "r" for text reading.

ord(c: str) int

Return the Unicode code point of a single-character string c as an integer.

pow(base: Any, exp: Any, mod: Any | None = None) Any

Return base raised to the power exp. If mod is given, return base ** exp % mod (computed more efficiently than the explicit form).

print(*objects: Any, sep: str = ' ', end: str = '\n', file: Any = None) None

Print objects to the text stream file, separated by sep and followed by end.

property(fget: Callable[[Any], Any] | None = None, fset: Callable[[Any, Any], None] | None = None, fdel: Callable[[Any], None] | None = None, doc: str | None = None) property

Return a property attribute. Typically used as a decorator to define managed attributes on a class.

range(*args: int) range

Return an immutable sequence of integers. Called as range(stop), range(start, stop) or range(start, stop, step).

repr(obj: Any) str

Return a string containing a printable representation of obj.

reversed(seq: Any) Iterator[Any]

Return a reverse iterator over the values of the given sequence.

round(number: Any, ndigits: int | None = None) Any

Return number rounded to ndigits decimal places. If ndigits is omitted, return the nearest integer.

class set(iterable: Iterable[Any] = ())

Return a new set object, optionally with elements taken from iterable.

setattr(obj: Any, name: str, value: Any) None

Set the named attribute on obj to value. The counterpart of getattr().

class slice

The slice builtin is the type that slice objects have.

sorted(iterable: Iterable[Any], key: Callable[[Any], Any] | None = None, reverse: bool = False) list

Return a new sorted list from the items in iterable.

staticmethod(func: Callable[..., Any]) staticmethod

Transform a method into a static method. Typically used as a decorator.

class str(object: Any = '', encoding: str = 'utf-8', errors: str = 'strict')

Return a string version of object. If object is a bytes-like object, the encoding and errors arguments control decoding.

sum(iterable: Iterable[Any], start: Any = 0) Any

Sum start and the items of iterable from left to right, and return the total.

super(type: type | None = None, obj_or_type: Any | None = None) Any

Return a proxy object that delegates method calls to a parent or sibling class of type. Useful for accessing inherited methods that have been overridden in a class.

class tuple(iterable: Iterable[Any] = ())

Build a new tuple, optionally populated from items in iterable. Tuples are immutable sequences.

type(obj: Any) type

With one argument, return the type of obj. The returned value is a type object.

zip(*iterables: Iterable[Any]) Iterator[tuple]

Return an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument iterables. Iteration stops when the shortest input iterable is exhausted.

Exceptions

exception AssertionError

Raised when an assert statement fails.

exception AttributeError

Raised when an attribute reference or assignment fails.

exception Exception

Common base class for all non-system-exiting exceptions.

exception ImportError

Raised when an import statement fails to find the module definition.

exception IndexError

Raised when a sequence subscript is out of range.

exception KeyboardInterrupt

Raised when the user interrupts program execution, usually by pressing Ctrl+C on the REPL.

See also in the context of Soft Bricking (failure to boot).

exception KeyError

Raised when a mapping (dictionary) key is not found in the set of existing keys.

exception MemoryError

Raised when an operation runs out of memory.

exception NameError

Raised when a local or global name is not found.

exception NotImplementedError

Raised when an abstract method or unimplemented feature is invoked.

exception OSError

Raised when a system function returns a system-related error.

exception RuntimeError

Raised when an error is detected that doesn’t fall in any of the other categories.

exception StopIteration

Raised by next() and an iterator’s __next__() method to signal that there are no further items.

exception SyntaxError

Raised when the parser encounters a syntax error.

exception SystemExit

Raised by sys.exit() to request interpreter termination. Unlike most exceptions, it does not produce a traceback when uncaught.

On non-embedded ports (i.e. Windows and Unix), an unhandled SystemExit exits the MicroPython process in a similar way to CPython.

On embedded ports, an unhandled SystemExit currently causes a Soft Reset of MicroPython.

exception TypeError

Raised when an operation or function is applied to an object of inappropriate type.

exception ValueError

Raised when a built-in operation or function receives an argument of the right type but an inappropriate value.

exception ZeroDivisionError

Raised when the second argument of a division or modulo operation is zero.