Python Tutorial · Revision notes
Python Variables & Data Types
Variables store data. Data types decide what kind of data it is and what you can do with it. This chapter covers variables, built-in data types, numbers, type casting and the Boolean type.
Quick revision
- A variable is a name bound to an object; assignment creates it; no declaration needed.
- Naming: letters, digits, underscore; no leading digit; no keywords; case-sensitive; use snake_case.
- Built-in types: int, float, complex, str, bool, list, tuple, range, dict, set, frozenset, bytes, NoneType.
- Mutable: list, dict, set. Immutable: int, float, str, tuple, frozenset, bool.
- int is unlimited; float is approximate (0.1 + 0.2 trap); use Decimal or isclose.
- int() truncates; int("3.5") fails; input() returns str.
- Falsy: 0, 0.0, "", [], (), {}, set(), None, False; all others are truthy.
- bool is a subclass of int; all([]) is True, any([]) is False.