Type checks
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'NoneType'>
<class 'bool'>Use isinstance to test whether a value belongs to a type.
Integer
One int type with a signed 128-bit range, from -(2**127) to 2**127 - 1. Going out of range raises OverflowError. Details: Limits and errors. There is no complex type.
170141183460469231731687303715884105727
overflowFloat
IEEE-754 double precision. Mixing int and float in arithmetic yields a float.
False
True
5.0
2
0
1.6Boolean
bool has two values, True and False. It is a subclass of int.
True
2Which values count as true: Truthy and falsy below.
String
Immutable sequence of Unicode code points. Indexing returns a one-character string. len counts code points, not bytes. Iteration yields characters.
h o
éll
5
héllo world
héllohéllo
TrueLiteral forms, quotes, and escapes: Syntax. Methods: Methods.
Bytes
Immutable sequence of octets, each 0 to 255. bytes stores raw data, not text. Indexing returns an int, and iteration yields ints.
b'hello'
5
104
b'ell'
97
98
99bytes never equals str, even for valid UTF-8. Non-ASCII characters in a bytes literal are stored as their UTF-8 encoding.
False
b'\xc3\xa9'Encoding round-trips use str.encode and bytes.decode.
b'Edge Python' Edge PythonConstructor forms (bytes(), bytes(n), from an int iterable, from an encoded string): Builtins. Methods such as hex, split, and fromhex: Methods.
List
Mutable sequence. Assignment copies the reference, so aliases share mutations.
[99, 2, 3, 4]
4
[99, 2, 3, 4, 5]Equality is structural.
True
TrueSlice assignment with step 1 resizes the list in place. Slice deletion removes a range. Assigning into an empty slice inserts.
[1, 20, 30, 40, 4, 5]
[1, 20, 4, 5]
[1, 99, 20, 4, 5]+= on a list extends it in place, so aliases see it.
[1, 2, 3, 4]Tuple
Immutable sequence. A tuple is hashable when its elements are, which makes it the usual compound dict key.
1
(1, 2, 3, 4, 5)
keyLiteral forms such as the trailing comma in (1,): Syntax.
Dict
Insertion-ordered mapping. Keys must be hashable: numbers, strings, bytes, bools, None, frozensets, and tuples of hashables. An unhashable key raises TypeError. Numerically equal keys collapse, so 1, 1.0, and True are one key and the latest assignment wins. Iteration yields keys.
1
{'a': 1, 'b': 2, 'c': 3}
{1: 'bool'}
x
yunhashable keyMethods such as keys, values, items, and get: Methods.
Set
Unordered collection of unique, hashable values. The operators |, &, -, and ^ build new sets. The augmented forms |=, &=, and ^= update the set in place, so aliases observe the change. <= and < test subsets.
4
[1, 2, 3, 4]
[1, 2, 3, 4]
[2, 3]
True
[1, 2, 3, 4, 5]The empty set is set(), since {} makes a dict (see Syntax). Named operations such as union and issubset: Methods.
Frozenset
Immutable, hashable set built with frozenset(iterable). It supports len, iteration, membership, unpacking, the algebra operators, and the subset comparisons. In mixed set and frozenset algebra the result takes the type of the left operand. A frozenset has no named methods. frozenset({1}).union({2}) raises AttributeError. Convert with set(fs) for the method API.
3
True
[1, 2, 3]
<class 'frozenset'>
<class 'set'>
[1, 2, 3, 4]
True
okUnpacking in literals
* spreads an iterable into a list or set literal. ** spreads a mapping into a dict literal. Both 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), or 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 and is distinct from the string '...'.
Ellipsis
True
<class 'ellipsis'>
FalseConversions
Every constructor (int, float, str, bool, list, tuple, set) doubles as a converter. The full matrix: Builtins. The two gotchas worth remembering:
3
-3
False
TrueTruthy and falsy
Falsy values (everything else is truthy):
| Falsy values |
|---|
None |
False |
0, 0.0 |
"", b"" |
[], () |
{}, set(), frozenset() |
range(0) |
False
False
False
False
True
True