In this article, we will learn about various Python operators divided into the following categories:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic operators in Python
Operator Name Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b
** Exponentiation a ** b
// Floor division a // b
Assignment operators in Python
| Operator | Example | Equivalent to |
|---|---|---|
| = | a = 5 | a = 5 |
| += | a += 3 | a= a + 3 |
| -= | a -= 3 | a = a - 3 |
| *= | a *= 3 | a = a * 3 |
| /= | a /= 3 | a= a / 3 |
| %= | a %= 3 | a = a % 3 |
| //= | a //= 3 | a = a // 3 |
| **= | a **= 3 | a = a ** 3 |
| &= | a &= 3 | a = a & 3 |
| |= | a |= 3 | a = a | 3 |
| ^= | a^= 3 | a= a ^ 3 |
| >>= | a >>= 3 | a = a >> 3 |
| <<= | a <<= 3 | a = a << 3 |
Comparison operators in Python
| Operator | Name | Example |
|---|---|---|
| == | Equal | a == b |
| != | Not equal | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
Logical operators in Python
| Operator | Description | Example |
|---|---|---|
| AND | Returns True if both statements are true | a < 4 and a < 11 |
| OR | Returns True if one of the statements is true | a < 7 or x < 3 |
| NOT | Reverse the result, returns False if the result is true | not(a < 1 and a < 5) |
Identity operators in Python
| Operator | Description | Example |
|---|---|---|
| is | Returns true if both variables are the same object | a is b |
| is not | Returns true if both variables are not the same object | a is not b |
Membership operators in Python
| Operator | Description | Example |
|---|---|---|
| in | Returns True if a sequence with the specified value is present in the object | a in b |
| not in | Returns True if a sequence with the specified value is not present in the object | a not in b |
Bitwise operators in Python
| Operator | Name | Description |
|---|---|---|
| & | AND | Sets each bit to 1 if both bits are 1 |
| | | OR | Sets each bit to 1 if one of two bits is 1 |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
| ~ | NOT | Inverts all the bits |
| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |