Python Built-in Types
Python has the following built-in types available at the global scope.
Contents
Bool
An object referencing one of two possible values: True and False.
Variables instantiated as bool are mutable but the boolean values themselves are immutable.
ByteArray
A mutable bytes object. The following are equivalent declarations:
ba = bytearray(b'...')) ba = bytearray([46, 46, 46]).
Note that iterating over a bytearray yeilds int values, not byte values.
In the context of type annotations, try:
ba: bytes = bytearray(b'abc'))
Bytes
In the context of type annotations, try:
b: bytes = b"abc"
Complex
A complex number stored as real and imaginary components.
z = complex(-2.0, 0.0) z == z.real + z.imag*1j
See the cmath module for doing math with complex numbers.
Dict
In the context of type annotations, try:
d: dict[str, int] = {"1": 1}
Float
A floating point number.
There are also the IEEE 754 special values:
float("NaN") representing not a number
float("inf") representing positive infinity
float("-inf") representing negative infinity
In the context of type annotations, try:
f: float = 3.14
FrozenSet
In the context of type annotations, try:
def __init__(self) --> None: self._options: frozenset[str] = frozenset([1, 2])
Int
In the context of type annotations, try:
i: int = 0
List
In the context of type annotations, try:
l: list[str] = ["ham", "spam"]
MemoryView
An interface into internal memory of objects.
In the context of type annotations, try:
mv: bytes = memoryview(obj)
Set
In the context of type annotations, try:
s: set[str] = set(["A", "B", "A"])
Str
In the context of type annotations, try:
s: str = "abc"
A list of methods for str objects can be seen here.
Tuple
In the context of type annotations, try:
single: tuple[int] = [1] double: tuple[int, int] = [1, 2] nple: tuple[int, ...] = [1, 2, 3, 4, 5]
The tuple[T, ...] annotation checks for homogenous typing. To allow for a a variable number of any values, try:
from typing import Any something: tuple[Any, ...] = [1, "1"]