Type checks
print(type(42))
print(type(3.14))
print(type("hi"))
print(type([1, 2]))
print(type((1, 2)))
print(type({1, 2}))
print(type({"a": 1}))
print(type(None))
print(type(True))<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 that auto-promotes to wide integers, capped at ±2¹²⁷ (full mechanics in Integer width). No CPython unbounded ints; no complex (1j, 2+3j).
# Modular exponentiation
print(pow(7, 13, 19))
print(divmod(17, 5))7
(3, 2)Float
IEEE-754 double precision. Mixed arithmetic with int coerces to float.
print(0.1 + 0.2 == 0.3)
print(-0.0 == 0.0)
print(1 / 3)
print(round(2.5)) # banker's rounding
print(round(0.5))
print(round(1.55, 1))False
True
0.3333333333333333
2
0
1.6String
Strings are immutable. Indexing returns a single-character string.
s = "hello"
print(s[0], s[-1])
print(s[1:4])
print(len(s))
print(s + " world")
print(s * 2)
print("ll" in s)h o
ell
5
hello world
hellohello
TrueIteration yields characters:
for ch in "abc":
print(ch)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: stores raw octets, not Unicode. Indexing returns an int, not a single-byte slice.
data = b"hello"
print(data)
print(type(data))
print(len(data))
print(data[0]) # int, the byte value
print(data[1:4]) # bytes, sliceb'hello'
<class 'bytes'>
5
104
b'ell'# Hex escapes for arbitrary bytes
raw = b"\x00\x01\xff"
print(raw)
print(raw.hex())b'\x00\x01\xff'
0001ff# Iteration yields ints, not bytes
for byte in b"abc":
print(byte)97
98
99# Constructors
print(bytes()) # empty
print(bytes(3)) # zero-filled, length 3
print(bytes([65, 66, 67])) # from int iterable
print(bytes("hi", "utf-8")) # encoded stringb''
b'\x00\x00\x00'
b'ABC'
b'hi'# Round-tripping with str
s = "Edge Python"
encoded = s.encode("utf-8")
decoded = encoded.decode("utf-8")
print(encoded, decoded)b'Edge Python' Edge Pythonbytes is hashable and comparable to other bytes; bytes == str is always False (even for valid UTF-8). Methods: decode, hex, startswith, endswith. Encodings: "utf-8" (default), "ascii".
List
Mutable sequence.
xs = [1, 2, 3]
xs[0] = 99
xs.append(4)
print(xs)
print(len(xs))
# Aliasing, both names see mutation
ys = xs
ys.append(5)
print(xs)[99, 2, 3, 4]
4
[99, 2, 3, 4, 5]# Equality is structural
print([1, 2, 3] == [1, 2, 3])
print([1, [2, 3]] == [1, [2, 3]])True
True# Slice assignment (step=1) resizes the list in place
xs = [1, 2, 3, 4, 5]
xs[1:3] = [20, 30, 40]
print(xs)
# Slice deletion
del xs[2:4]
print(xs)
# Insertion via empty slice
xs[1:1] = [99]
print(xs)[1, 20, 30, 40, 4, 5]
[1, 20, 4, 5]
[1, 99, 20, 4, 5]Tuple
Immutable sequence. Fastest container for fixed-size data; the only one usable as a dict key in mixed-type cases.
t = (1, 2, 3)
print(t[0])
print(t + (4, 5))
print((1,)) # one-element needs trailing comma
print(()) # empty1
(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, second insertion overwrites.
d = {"a": 1, "b": 2}
print(d["a"])
d["c"] = 3
print(d)
print(list(d.keys()))
print(list(d.values()))
print(list(d.items()))1
{'a': 1, 'b': 2, 'c': 3}
['a', 'b', 'c']
[1, 2, 3]
[('a', 1), ('b', 2), ('c', 3)]# Iteration yields keys
for k in {"x": 1, "y": 2}:
print(k)x
ySet
Unordered, no duplicates, hashable values. Mutators (add, remove, discard, pop, clear, update) and algebraic operators (|, &, -, ^ and named methods); see Methods.
s = {1, 2, 2, 3}
s.add(4)
print(s)
print(len(s))
# Empty set literal is set(), not {}
print(set())
print(type({})) # this is a dict
# Algebra
print({1, 2, 3} | {3, 4})
print({1, 2, 3} & {2, 3, 4})
print({1, 2} <= {1, 2, 3}) # subset{2, 3, 4, 1}
4
set()
<class 'dict'>
{3, 1, 4, 2}
{3, 2}
TrueRange
Lazy integer sequence. range(stop), range(start, stop), range(start, stop, step).
print(list(range(5)))
print(list(range(2, 8)))
print(list(range(0, 10, 2)))
print(list(range(10, 0, -1)))[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.
print(None)
print(None is None)
print(type(None))None
True
<class 'NoneType'>Ellipsis
... is a singleton of type ellipsis. Compares equal only to itself; distinct from '...'.
print(...)
print(... is ...)
print(type(...))
print(... == '...')Ellipsis
True
<class 'ellipsis'>
FalseConversions
print(int("42"))
print(int(3.7)) # truncates toward zero
print(int(True))
print(float("3.14"))
print(str(42))
print(str([1, 2]))
print(bool([])) # empty is falsy
print(bool([0])) # non-empty is truthy
print(list("abc"))
print(tuple([1, 2, 3]))
print(set([1, 1, 2]))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) |