Chapter 4 of 15All chapters
Chapter 4 of 15
Data types
The built-in types and how to check one.
The everyday types
Numbers come as int and float, text as str, truth values as bool, and nothing as None. The containers are list, tuple, dict and set.
- type(x) returns the type, and isinstance(x, int) is the check to prefer in code.
- bool is a subclass of int, so True behaves as 1 in arithmetic.
Converting
Each type doubles as a converter: int("7") gives 7, str(7) gives "7", and list("abc") gives a list of characters. A conversion that cannot work raises ValueError rather than guessing.