Python Bitwise Operators [Full Guide + Videos]

Bitwise operators perform operations on the binary (bit) representation of integers. Background: Each integer is first written as a binary number that is a sequence of digits 0 or 1. For example: 0 is written as “0” 1 is written as “1” 2 is written as “10” 3 is written as “11” 4 is written … Read more

Python Logical Operators [Blog + Video]

Logical operators work on Boolean values but can be used on integers and other objects as well. Python has three logical operators: and, or, and not. The following table provides a quick overview of Python logical operators: Operator Description Example and Returns True if both operands are True, and False otherwise. (True and True) == … Read more

Python Arithmetic Operators

Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers. Operator Name Description Example + Addition Calculating the sum of the two operands 3 + 4 == 7 – Subtraction Subtracting the second operand from the first operand 4 – 3 == 1 * Multiplication Multiplying the first with the second operand 3 … Read more

Python Comparison Operators [Blog + Videos]

Comparison operators are applied to comparable objects and they return a Boolean value (True or False). Operator Name Description Example > Greater Than Returns True if the left operand is greater than the right operand 3 > 2 == True < Less Than Returns True if the left operand is smaller than the right operand … Read more

Python In-Place Assignment Operators

In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, x += 3 is the same as x = x + 3 of … Read more

Python In-Place Bitwise Left-Shift Operator

Python’s in-place bitwise left-shift operator x <<= y calculates the left-shift operation x << y, and assigns the result to the first operands variable name x. You can set up the in-place left-shift behavior in your own class by overriding the magic “dunder” method __ilshift__(self, other) in your class definition. The expression x <<= y … Read more

Python In-Place Bitwise XOR Operator

Python’s in-place bitwise XOR operator x ^= y calcualtes bitwise XOR x ^ y and assigns the result to the first operand x. To set this up for your own class, override the magic “dunder” method __ixor__(self, other) in your class definition. The expression x ^= y is syntactical sugar for the longer-form x = … Read more

Python In-Place Bitwise Right-Shift Operator

Python’s in-place bitwise right-shift operator x >>= y calculates the right-shift operation x >> y, and assigns the result to the first operands variable name x. You can set up the in-place right-shift behavior in your own class by overriding the magic “dunder” method __irshift__(self, other) in your class definition. The expression x >>= y … Read more

Python In-Place Bitwise AND Operator

Python’s in-place bitwise AND operator x &= y calcualtes bitwise-and x & y and assigns the result to the first operand x. To set it up for your own class, override the magic “dunder” method __iand__(self, other) in your class definition. The expression x &= y is syntactical sugar for the longer-form x = x … Read more

Python In-Place Exponentiation Operator

Python provides the in-place exponentiation operator x **= y that raises x to the power of y using x ** y and assigns the result to the first operands variable name x. You can set up the in-place exponentiation behavior for your own class by overriding the magic “dunder” method __ipow__(self, other) in your class … Read more

Python In-Place Integer Division Operator

Python’s in-place integer 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 integer (or floor) division for your own class by overriding the magic “dunder” method __floordiv__(self, other) in your class definition. The expression x /= … Read more

Python In-Place Modulo Operator

Python provides the operator x %= y to calculate the modulo operation x % y, and assign the result in-place to the first operands variable x. You can set up the in-place modulo behavior for your own class by overriding the magic “dunder” method __imod__(self, other) in your class definition. The expression x %= y … Read more