# Python Operators

## 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 …

## 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 …

## 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 …

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 …

## Python Bitwise Left-Shift << Operator

The Python bitwise left-shift operator x << n shifts the binary representation of integer x by n positions to the left. For a positive integer, it inserts a 0 bit on the right and shifts all remaining bits by one position to the left. For example, if you left-shift the binary representation 0101 by one …

## Python Bitwise Right-Shift >> Operator

The Python bitwise right-shift operator x >> n shifts the binary representation of integer x by n positions to the right. It inserts a 0 bit on the left and removes the right-most bit. For example, if you right-shift the binary representation 0101 by one position, you’d obtain 0010. Semantically, the bitwise right-shift operator is …

## Python Bitwise NOT Operator

Python’s bitwise NOT operator ~x inverts each bit from the binary representation of integer x so that 0 becomes 1 and 1 becomes 0. This is semantically the same as calculating ~x == -x-1. For example, the bitwise NOT expression ~0 becomes -1, ~9 becomes -10, and ~32 becomes -33. As you go over the …

## Python Bitwise XOR ^ Operator

Python’s bitwise XOR operator x ^ y performs logical XOR on each bit position on the binary representations of integers x and y. Each output bit evaluates to 1 if and only if exactly one of the two input bits at the same position are 1. For example, the integer expression 4 ^ 3 is …

## Python Bitwise OR | Operator

Python’s bitwise OR operator x | y performs logical OR on each bit position on the binary representations of integers x and y. Each output bit evaluates to 1 if and only if at least one of the two input bits at the same position are 1. For example, the integer expression 4 | 3 …

## Python Bitwise AND Operator &

Python’s bitwise AND operator x & y performs logical AND on each bit position on the binary representations of integers x and y. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0. For example, the integer expression 4 & 3 is translated to binaries 0100 …

## 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 …

## Python Integer Division [2-Min Tutorial]

When I started to learn Python 3, I used to be confused about the semantics of dividing two integers. Is the result a float or an integer value? The reason for my confusion was a nasty Java bug that I once found in my code. The code was supposed to perform a simple division of …