Python is a very rich language which makes it a bit hard to memorize everything, including operators.
If you want to learn more about operators then keep reading 🙂
Mathematical Operators (Arithmetic)
Name | Syntax in Python | Example |
Addition | + | 2+1 |
Substraction | – | 2-1 |
Multiplication | * | 2*1 |
Division | / | 2/1 |
Modulus | % | 2%1 |
Exponentiation | ** | 2**2 |
Floor Division | // | 2//1 |
Comparison Operators
Name | Syntax in Python | Example |
Equal | == | a == b |
Not Equal | != | a != b |
Less than | < | a < b |
Greater than | > | a > b |
Less than or equal | <= | a <= b |
Greater than or equal | >= | a >= b |
Assignment Operators
Operator | Similar to | Usage |
= | a = 6 | a = 6 |
+= | a = a + 6 | a += 6 |
-= | a = a – 6 | a -= 6 |
*= | a = a * 6 | a *= 6 |
/= | a = a / 6 | a /= 6 |
%= | a = a % 6 | a %= 6 |
//= | a = a // 6 | a //=6 |
**= | a = a**6 | a **= 6 |
Logical Operators
Name & Syntax | Description | Example |
and | Checks 2 statements, if both are true then it returns true. | a and b |
or | Checks 2 statements, if one of them is true then it returns true. | a or b |
not | Checks 2 statements, If the result is true then it returns false (reverse). | not (a and b) |
Identity Operators
Name and Syntax | Description | Example |
is | Checks 2 objects, returns true if they are the same. | a is b |
is not | Checks 2 objects, returns true if they are NOT the same. | a is not b |
Membership Operators
Name and Syntax | Description | Example |
in | Checks 2 objects, returns true if a sequence with the same value is present in both objects. | a in b |
not in | Checks 2 objects, returns false if a sequence with the same value is present in both objects. | a not in b |
Binary Comparison Operators (Bitwise)
Name | Syntax in Python | Example |
AND | & | a & b |
OR | | | a | b |
XOR | ^ | a ^ b |
NOT | ~ | a ~ b |
Left Shift | << | a << b |
Right Shift | >> | a >> b |