Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Operators are special symbols or keywords in Python that execute specific mathematical, relational, or logical computations on one or more operands. At the language level, Python operators act as syntactic sugar for underlying special methods (often referred to as “dunder” or magic methods) defined on the operand objects.

Arithmetic Operators

Perform standard mathematical operations. They map directly to Python’s numeric protocol methods.
a = 5
b = 2

a + b   # Maps to a.__add__(b)
a - b   # Maps to a.__sub__(b)
a * b   # Maps to a.__mul__(b)
a / b   # Maps to a.__truediv__(b) (True division, always returns a float)
a // b  # Maps to a.__floordiv__(b) (Floor division, rounds towards negative infinity)
a % b   # Maps to a.__mod__(b) (Modulo, returns remainder)
a ** b  # Maps to a.__pow__(b) (Exponentiation)
-a      # Maps to a.__neg__() (Unary negation)
+a      # Maps to a.__pos__() (Unary positive)


# Matrix Multiplication (introduced in Python 3.5)
class Matrix:
    def __matmul__(self, other):
        return "matrix_mult"

m1 = Matrix()
m2 = Matrix()
m1 @ m2 # Maps to m1.__matmul__(m2)

Comparison (Relational) Operators

Evaluate the relationship between two operands, returning a boolean value. Python supports chaining comparison operators, which implicitly applies a logical and between the evaluations.
a = 5
b = 5
c = 10

a == b     # Maps to a.__eq__(b)
a != b     # Maps to a.__ne__(b)
a > b      # Maps to a.__gt__(b)
a < b      # Maps to a.__lt__(b)
a >= b     # Maps to a.__ge__(b)
a <= b     # Maps to a.__le__(b)
a <= b < c # Chained comparison, equivalent to (a <= b) and (b < c)

Logical Operators

Perform boolean logic. Python utilizes short-circuit evaluation for and and or. These operators do not strictly return booleans; they return the last evaluated operand.
a = True
b = False

a and b  # Returns 'a' if 'a' is falsy; otherwise evaluates and returns 'b'
a or b   # Returns 'a' if 'a' is truthy; otherwise evaluates and returns 'b'
not a    # Evaluates a.__bool__() or a.__len__(). If neither is defined, the object defaults to truthy. Strictly returns a boolean.

Bitwise Operators

Perform bit-level operations on integers. They manipulate the underlying binary representation of the operands.
a = 5  # Binary: 0101
b = 3  # Binary: 0011

a & b   # Maps to a.__and__(b) (Bitwise AND)
a | b   # Maps to a.__or__(b) (Bitwise OR)
a ^ b   # Maps to a.__xor__(b) (Bitwise XOR)
~a      # Maps to a.__invert__() (Bitwise NOT / One's complement)
a << b  # Maps to a.__lshift__(b) (Left shift by 'b' bits)
a >> b  # Maps to a.__rshift__(b) (Right shift by 'b' bits)

Assignment Operators

Bind values to variables. Augmented assignment operators combine an arithmetic or bitwise operation with assignment. They attempt to modify the object in-place if the object is mutable.
b = 10

a = b    # Standard reference binding
a += b   # Maps to a.__iadd__(b) (In-place addition, falls back to __add__ if undefined)
a -= b   # Maps to a.__isub__(b)

# Note: Augmented assignment exists for all arithmetic and bitwise operators (*=, /=, &=, etc.)

(c := b) # Assignment expression (Walrus operator), binds 'c' to 'b' and returns the value

Identity Operators

Evaluate whether two references point to the exact same object in memory. This is an evaluation of the CPython memory address, distinct from object value equality (==).
a = [1, 2, 3]
b = a
c = [1, 2, 3]

a is b      # Equivalent to id(a) == id(b), evaluates to True
a is not c  # Equivalent to id(a) != id(c), evaluates to True

Membership Operators

Evaluate whether a specific value exists within a sequence or collection.
a = 2
b = [1, 2, 3]

a in b      # Maps to b.__contains__(a). If missing, falls back to iterating via __iter__() or __getitem__()
a not in b  # Evaluates to not (a in b)

The operator Module

Python provides the built-in operator module, which exports a set of efficient, C-implemented functions corresponding to the intrinsic operators. This is utilized primarily in functional programming paradigms where passing an operator as a callable is required.
import operator

a = 5
b = 2
seq = [10, 20, 30]

class MockObject:
    x = 100

obj = MockObject()

operator.add(a, b)        # Equivalent to a + b
operator.eq(a, b)         # Equivalent to a == b
operator.contains(seq, a) # Equivalent to a in seq
operator.is_(a, b)        # Equivalent to a is b


# Specialized getters for functional mapping
get_second = operator.itemgetter(1)   # Equivalent to lambda x: x[1]
get_second(seq)                       # Returns 20

get_x = operator.attrgetter('x')      # Equivalent to lambda obj: obj.x
get_x(obj)                            # Returns 100
Master Python with Deep Grasping Methodology!Learn More