Type checks
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'NoneType'>
<class 'bool'>For runtime membership in a type, use isinstance.
Integer
One int type, transparently two-tier. A fast inline path auto-promotes to wide integers, capped at ±2¹²⁷ (full mechanics in Integer width). No CPython unbounded ints. No complex (1j, 2+3j).
7
(3, 2)Float
IEEE-754 double precision. Mixed arithmetic with int coerces to float.
False
True
0.3333333333333333
2
0
1.6String
Strings are immutable. Indexing returns a single-character string.
h o
ell
5
hello world
hellohello
TrueIteration yields characters:
a
b
clen(s) and padding (str.center / str.zfill) measure in code points, not bytes. 'ñ'.center(5, '*') -> '**ñ**' (5 visual chars).
Bytes
Immutable sequence of bytes (each 0-255). Distinct from str: it stores raw octets, not Unicode. Indexing returns an int, not a single-byte slice.
b'hello'
<class 'bytes'>
5
104
b'ell'b'\x00\x01\xff'
0001ff97
98
99b''
b'\x00\x00\x00'
b'ABC'
b'hi'b'Edge Python' Edge Pythonbytes is hashable and comparable to other bytes. bytes == str is always False, even for valid UTF-8. Methods include decode, hex, find, count, replace, split, startswith, endswith, lower, upper, strip, join, and the bytes.fromhex classmethod (see Methods). Encodings: "utf-8" (default), "ascii".
List
Mutable sequence.
[99, 2, 3, 4]
4
[99, 2, 3, 4, 5]True
True[1, 20, 30, 40, 4, 5]
[1, 20, 4, 5]
[1, 99, 20, 4, 5]+= on a list extends in place, so aliases see it:
[1, 2, 3, 4]Tuple
Immutable sequence. It is the fastest container for fixed-size data. It is also the usual hashable container for compound dict keys (frozensets also work).
1
(1, 2, 3, 4, 5)
(1,)
()Dict
Insertion-ordered mapping. Keys must be hashable: numbers, strings, bytes, bools, None, frozensets, tuples of hashables. Mutable containers as keys -> TypeError: unhashable type. Numerically equal keys (1/1.0, True/1) collapse. The second insertion overwrites.
1
{'a': 1, 'b': 2, 'c': 3}
['a', 'b', 'c']
[1, 2, 3]
[('a', 1), ('b', 2), ('c', 3)]x
ySet
Unordered, no duplicates, hashable values. Mutators (add, remove, discard, pop, clear, update) and algebraic operators (|, &, -, ^ and named methods). See Methods.
{2, 3, 4, 1}
4
set()
<class 'dict'>
{3, 1, 4, 2}
{3, 2}
TrueFrozenset
Immutable, hashable set. Build with frozenset(iterable). Supports len, iteration, membership (in), tuple-unpacking (a, b = fs), the algebra operators | & - ^, the subset/superset comparisons < <= > >= == !=, and use as a dict key or set element. In mixed set / frozenset algebra the result takes the left operand’s type (frozenset | set is a frozenset, set | frozenset is a set).
It has no methods: the named set operations (union, intersection, difference, symmetric_difference, issubset, issuperset, isdisjoint) and copy raise AttributeError. Use the operators, or convert with set(fs) for the named-method / mutating API.
3
True
[1, 2, 3]
[1, 2, 3, 4]
[2, 3]
True
okUnpacking in literals
* spreads an iterable into a list/set literal. ** spreads a mapping into a dict literal. Mix freely with regular elements. For dicts, later keys win.
[1, 2, 3, 1, 2]
{1, 2, 3}
{'x': 9, 'y': 2}Range
Lazy integer sequence. range(stop), range(start, stop), range(start, stop, step).
[0, 1, 2, 3, 4]
[2, 3, 4, 5, 6, 7]
[0, 2, 4, 6, 8]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]NoneType
Single value, single type.
None
True
<class 'NoneType'>Ellipsis
... is a singleton of type ellipsis. It compares equal only to itself. It is distinct from '...'.
Ellipsis
True
<class 'ellipsis'>
FalseConversions
42
3
1
3.14
42
[1, 2]
False
True
['a', 'b', 'c']
(1, 2, 3)
{2, 1}Truthy and falsy
Falsy values (everything else is truthy):
| Falsy values |
|---|
None |
False |
0, 0.0 |
"" (empty string) |
[], () |
{}, set() |
range(0) |