Python's type system has many surprises. bool is a subclass of int. Floats lie. None is not False. Integer caching is real. Test yourself.
print(type(True)) print(isinstance(True, int))
bool is a subclass of int. True and False are instances of bool, which inherits from int. So isinstance(True, int) returns True. This is a fundamental Python design choice — booleans ARE integers.
print(True + True) print(True + True + True) print(True * 5) print(False * 100)
bool is a subclass of int, arithmetic works directly: True == 1 and False == 0. So True + True is 1 + 1 = 2. This is often used in Python to count how many items in a list are truthy: sum(x > 0 for x in nums).
print(True == 1) print(True == 1.0) print(True is 1)
== checks value equality. Since True == 1 == 1.0, both first two are True.is checks object identity (same memory address). True and the integer 1 are different objects in memory, so True is 1 is False. Never use is to compare values — use ==.
print(0.1 + 0.2 == 0.3)
0.1 + 0.2 equals 0.30000000000000004, not 0.3. Neither 0.1, 0.2, nor 0.3 can be represented exactly in binary.abs(0.1 + 0.2 - 0.3) < 1e-9 or use math.isclose(0.1 + 0.2, 0.3).
print(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 1.0)
0.1 ten times does NOT equal 1.0 due to cumulative binary rounding errors. This is a critical lesson for anyone doing numerical computing in Python — use the decimal module for exact decimal arithmetic.
print(type(1/2)) print(type(1//2)) print(1/2) print(1//2)
/ is always true division — it always returns a float, even when dividing two integers. This is a key difference from C, where 1/2 == 0.// is floor division — it returns an int when both operands are integers, always rounding toward negative infinity.
print(-7 // 2)
// is floor division — it always rounds toward negative infinity, not toward zero. -7 / 2 = -3.5, and flooring -3.5 gives -4.-7 / 2 == -3. Python is different — and mathematically more consistent.
print(bool([])) print(bool([0])) print(bool({})) print(bool({0: 0}))
[] and {} evaluate to False. A non-empty container is truthy — even [0] (a list containing zero) is True, because the list itself is not empty.print(None == False) print(None == 0) print(None is None) print(bool(None))
None is not equal to False, 0, or anything else — it is its own unique value. None is None is always True because there is only one None object in Python.bool(None) is False — None is falsy — but it is not the same as False. Always use if x is None, not if x == None.
x = 256 y = 256 print(x is y) a = 257 b = 257 print(a is b)
x = 256 and y = 256, both variables point to the same cached object — x is y is True.a is b is False. This is a CPython implementation detail — never rely on is for integer comparison!
x = 10 y = 10 print(x is y) # small int, cached a = -6 b = -6 print(a is b) # outside cache range
-5 to 256 inclusive. 10 is in range so both x and y point to the same object. -6 is below -5, so each assignment creates a new object — different identity.
print(int(3.9)) print(int(-3.9)) print(int(3.1))
int() truncates toward zero — it simply chops off the decimal part. It does NOT round. So int(3.9) is 3, not 4. And int(-3.9) is -3, not -4. This differs from // (floor division), which rounds toward negative infinity.
print(round(0.5)) print(round(1.5)) print(round(2.5)) print(round(3.5))
0.5 → 0 (0 is even), 1.5 → 2 (2 is even), 2.5 → 2 (2 is even), 3.5 → 4 (4 is even).a = b = [] a.append(42) print(b)
a = b = [] creates one list and makes both a and b point to it. There is only one list object in memory. When you a.append(42), you are mutating that single shared list. So b also sees the change.a = [] then b = [] on separate lines.
x = y = z = 0 x += 1 print(x, y, z)
x += 1 does NOT modify the integer 0 in place. It creates a new integer 1 and rebinds x to it. y and z still point to the original 0.a.append() mutates the object in place. x += 1 on an integer rebinds the variable. These are fundamentally different operations.
x = [1, 2, 3] y = x x = [4, 5, 6] # rebind x, not mutate print(y)
y = x makes y point to the same list as x. Then x = [4, 5, 6] creates a brand new list and rebinds x to it. y still points to the original [1, 2, 3].x.clear(); x.extend([4,5,6]) — that would mutate the list and y would also change.
x = 10 ** 309 print(x)
10 ** 309 prints a 310-digit number without any error. This is one of Python's most programmer-friendly features.
print(type(True + 1)) print(type(True + 1.0)) print(type(True + False))
bool + int → int (bool is a subclass of int, result stays int). bool + float → float (float is wider, so the result is promoted to float). bool + bool → int (two bools add up to an int, not a bool).
x = "5" y = 2 print(x * y)
string * integer means string repetition. "5" * 2 produces "55", not 10. This is NOT type coercion — Python does not automatically convert "5" to 5.10, you need: int(x) * y.
x = float('inf') print(x > 10 ** 100) print(x + 1 == x) print(x - x)
float('inf') is positive infinity — larger than any finite number. Adding anything to infinity gives infinity, so x + 1 == x is True. Subtracting infinity from infinity is undefined — the result is nan (Not a Number).float('-inf') (negative infinity) and float('nan'). Check with math.isinf(x) and math.isnan(x).