Python In-Place Division Operator

Python’s in-place division operator x /= y divides two objects in-place by calculating x / y and assigning the result to the first operands variable name x. Set up in-place division for your own class by overriding the magic “dunder” method __truediv__(self, other) in your class definition. The expression x /= y is syntactical sugar … Read more

Python In-Place Multiplication Operator

Python provides the operator x *= y to multiply two objects in-place by calculating the product x * y and assigning the result to the first operands variable name x. You can set up the in-place multiplication behavior for your own class by overriding the magic “dunder” method __imul__(self, other) in your class definition. The … Read more

Python In-Place Subtraction Operator

Python provides the operator x -= y to subtract two objects in-place by calculating the difference x – y and assigning the result to the first operands variable name x. You can set up the in-place subtraction behavior for your own class by overriding the magic “dunder” method __isub__(self, other) in your class definition. The … Read more

Python In-Place Addition Operator

Python provides the operator x += y to add two objects in-place by calculating the sum x + y and assigning the result to the first operands variable name x. You can set up the in-place addition behavior for your own class by overriding the magic “dunder” method __iadd__(self, other) in your class definition. The … Read more

Python Exponent – 4 Operators Every Coder Must Know

Python has four ways to calculate the n-th power (exponent) of x so that xⁿ=x*x*…*x that multiplies the base x with itself, and repeating this n-times. Method 1: Use the double-asterisk operator such as in x**n. Method 2: Use the built-in pow() function such as in pow(x, n). Method 3: Import the math library and … Read more