For AI agents: use /llms.txt for the Nakafa content index.
Operator precedence determines how Python groups an expression when parentheses do not state the grouping explicitly. It is a parsing rule, not a promise that every operand will be evaluated in the same visual order.
Higher-precedence operators bind more tightly. Most binary operators at the same level group from left to right, but exponentiation groups from right to left and comparisons form chains. Parentheses are the clearest choice whenever the intended grouping might be missed.
Comparison operators are used to compare two values and produce boolean values (True or False). Comparison operators available in Python:
== equal to!= not equal to< less than<= less than or equal to> greater than>= greater than or equal toThese operators return a bool. Equality can compare objects of different types, but ordering comparisons such as < require types that define a compatible ordering and may raise TypeError otherwise. Comparisons share a precedence level and can be chained.
# Example of comparison operator usage
>>> 4 == 5
False
>>> 3 > 2.1 # Python supports this mixed numeric comparison
TrueBoolean operators allow you to combine multiple conditions or modify boolean values. Python provides three main boolean operators:
Operator and returns the first value if it's false, or the second value if the first value is true.
Operator or returns the first value if it's true, or the second value if the first value is false.
Operator not differs from and and or operators because it always produces a new boolean value.
# Example of boolean operators
>>> 4.0 and 5.0 # evaluates 4.0 as true, evaluates 5.0 as true, returns 5.0
5.0
>>> 0 and 5 # evaluates 0 as false, returns 0 (short-circuit)
0
>>> 4.0 or 5.0 # evaluates 4.0 as true, returns 4.0 (short-circuit)
4.0
>>> 0 or 5 # evaluates 0 as false, evaluates 5 as true, returns 5
5
>>> not 4.0 # evaluates 4.0 as true, returns False
False
>>> not 0 # evaluates 0 as false, returns True
TrueThe and and or operators use short-circuit evaluation, meaning evaluation stops when the result can be determined without evaluating all operands. Both return the last evaluated argument, while the not operator always creates a new boolean value.
Python has special rules for determining which values are considered False or True in boolean context.
Values considered False (Falsy):
False itselfNone (from NoneType)0 (integer zero)0.0 (float zero)0j (complex zero, where j is the imaginary unit)""[], {}, (), set()Values considered True (Truthy):
User-defined objects can customize truth testing with __bool__() or __len__(). If neither is defined, an instance is truthy by default.
The bool type in Python represents truth values False and True, is a subtype of integer (int), and booleans behave like and in mathematical operations.
# Bool constructor and arithmetic operations
>>> bool(-1)
True
>>> bool(0.0)
False
>>> True + True
2
>>> 3 * False
0You can combine comparison operators with boolean operators to create more complex conditions. Python also allows more natural comparison chaining like 1 < 2 < 3.
# Example of operator combination
>>> 4.0 > 3 and 2 >= 3 # ⇔ True and False
False
>>> 7 < 6 or 4 != 2 # ⇔ False or True
True
>>> not 0 < 2 # ⇔ not (0 < 2) ⇔ not True
False
# Comparison chaining
>>> 1 < 2 < 3 # ⇔ (1 < 2) and (2 < 3)
True
>>> 5 <= 7 < 10 # ⇔ (5 <= 7) and (7 < 10) --- application: interval test
TrueMany decimal fractions have no exact finite binary floating-point representation. Arithmetic rounds intermediate results, so two mathematically equivalent calculations can produce nearby but unequal floats.
# Floating point precision problem
>>> a = 0.01
>>> b = 0.1**2 # b is 0.010000000000000002
>>> a == b
False
# Solution with math.isclose()
>>> import math
>>> math.isclose(0.01, 0.1**2)
True
>>> math.isclose(100, 95, rel_tol=0.05) # relative tolerance is 5%
True
>>> math.isclose(100, 95, abs_tol=5) # absolute tolerance is 5
TruePython provides the math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0) function to test approximate equality. This function uses relative tolerance (rel_tol) and absolute tolerance (abs_tol) to determine whether two values are close enough.
Use exact equality when exact identity of the floating-point value is genuinely required. For measured or calculated quantities, choose tolerances from the problem domain and use
math.isclose(). Near zero, an appropriateabs_tolis essential because relative tolerance alone shrinks with the compared values.