Operators
Python provides the standard arithmetic operators: + (addition), - (subtraction), * (multiplication), / (true division), // (floor division), % (modulus), ...
Arithmetic Operators
Python provides the standard arithmetic operators: + (addition), - (subtraction), * (multiplication), / (true division), // (floor division), % (modulus), and ** (exponentiation). True division always returns a float: 7 / 2 gives 3.5. Floor division returns the largest integer not greater than the result: 7 // 2 gives 3. The modulus operator returns the remainder: 7 % 2 gives 1. Exponentiation: 2 ** 10 gives 1024.
Comparison and Logical Operators
Comparison operators return True or False: == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal), <= (less than or equal). Logical operators combine boolean expressions: and returns True only if both operands are True; or returns True if at least one is True; not inverts the boolean. Python allows chaining comparisons: 1 < x < 10 checks that x is between 1 and 10 exclusively, which is more readable than x > 1 and x < 10.
Assignment and Augmented Assignment
The basic assignment operator is =. Augmented assignment operators combine an operation with assignment: += adds and assigns (x += 3 is short for x = x + 3), -= subtracts and assigns, *= multiplies and assigns, /= divides and assigns, //= floor divides and assigns, %= modulo and assigns, **= raises to power and assigns. These make code more concise and are universally supported across Python data types that implement the relevant dunder methods.
Bitwise and Identity Operators
Bitwise operators work on the binary representations of integers: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift). These are used in low-level programming, flags, and performance-critical code. Identity operators check whether two names refer to the same object in memory: is returns True if they do, is not if they do not. Example: a = [1, 2]; b = a; a is b returns True because both names point to the same list object.
Operator Precedence
Python evaluates expressions based on operator precedence (order of operations). From highest to lowest: parentheses, exponentiation (**), unary + and -, multiplication/division/floor division/modulo, addition/subtraction, bitwise shifts, bitwise AND, bitwise XOR, bitwise OR, comparison operators, logical not, logical and, logical or. When in doubt, use parentheses to make intent explicit. For example, 2 + 3 * 4 evaluates to 14, not 20, because * has higher precedence than +.
Exam Tip
Two operator questions appear constantly in exams. First: what is the difference between == and is? The == operator checks value equality; is checks identity (same object in memory). print(1000 == 1000) is True, but two separately created integer objects with value 1000 may return False for is (Python caches small integers -5 to 256, so is works for those but not for larger values). Second: what does 5 // 2 return? It returns 2, an integer, not 2.5.