Cheat Sheets
Variables and Data Types · Study material
Variables and Data Types Cheat Sheet
| Code | Result / meaning |
|---|---|
a, b = 1, 2 / a, b = b, a | multiple assignment / swap |
first, *rest = seq | starred unpacking |
type(x), isinstance(x, (int, float)) | type, type check |
id(x), a is b | identity |
int("42"), int("ff", 16), int(-3.9) | 42, 255, -3 |
float("1e-3"), float("inf") | 0.001, infinity |
bin(10), oct(8), hex(255) | '0b1010', '0o10', '0xff' |
round(2.5), round(3.14159, 2) | 2, 3.14 |
bool(x) | truth value |
x is None | test for None |
math.isclose(a, b) | float comparison |
Decimal("0.10") | exact decimal (money) |
lst.copy(), copy.deepcopy(x) | shallow / deep copy |