Python __mod__() Magic Method

Syntax object.__mod__(self, other) The Python __mod__() method implements the modulo operation % that per default returns the remainder of dividing the left by the right operand. Internally, Python attempts to call x.__mod__(y) to implement the modulo operation x%y. If the method is not implemented, Python first attempts to call __rmod__ on the right operand and … Read more

Python __floordiv__() Magic Method

Syntax object.__floordiv__(self, other) The Python __floordiv__() method implements the integer division operation // called floor division—as opposed to the true division operation /. For example to evaluate the expression x // y, Python attempts to call x.__floordiv__(y). If the method is not implemented, Python first attempts to call __rfloordiv__ on the right operand and if … Read more

Python __truediv__() Magic Method

Syntax object.__truediv__(self, other) The Python __truediv__() method is called to implement the normal division operation / called true division—as opposed to the floor division operation //. For example to evaluate the expression x / y, Python attempts to call x.__truediv__(y). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To … Read more

Python __matmul__() Magic Method

Syntax object.__matmul__(self, other) The Python __matmul__() method is called to implement the matrix multiplication operation @. For example to evaluate the expression x @ y, Python attempts to call x.__matmul__(y). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check … Read more

Python __mul__() Magic Method

Syntax object.__mul__(self, other) The Python __mul__() method is called to implement the arithmetic multiplication operation *. For example to evaluate the expression x * y, Python attempts to call x.__mul__(y). We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check … Read more

Python __sub__() Magic Method

Syntax object.__sub__(self, other) Python’s object.__sub__(self, other) method returns a new object that represents the difference of two objects. It implements the subtraction operator – in Python. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat … Read more

Python __add__() Magic Method

Syntax object.__add__(self, other) Python’s object.__add__(self, other) method returns a new object that represents the sum of two objects. It implements the addition operator + in Python. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat … Read more

Python Operator Precedence

If you use multiple operators in a single expression, the semantics of that expression depends on the assumed operator precedence. For example, consider the expression 2 + 4 * 0. Does Python calculate (2 + 4) * 0 or 2 + (4 * 0)? Depending on the operator precedence, the result would be 0 or … Read more

Python is Operator — Checking Identity

The Python is keyword tests if the left and right operands refer to the same object—in which case it returns True. It returns False if they are not the same object, even if the two objects are equal. For example, the expression [1, 2, 3] is [1, 2, 3] returns False because although both lists … Read more