Python Operators
In Python, operators are special symbols used to perform operations on variables and values. You’ve already seen some, like +
and *
.
1. Types of Operators in Python
Python supports the following types of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
2. Arithmetic Operators
Used for basic mathematical operations.
Operator | Name | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 10 / 2 | 5.0 |
// | Floor Division | 10 // 3 | 3 |
% | Modulus | 10 % 3 | 1 |
** | Exponent | 2 ** 3 | 8 |
Example:
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
3. Assignment Operators
Used to assign values to variables.
Operator | Example | Equivalent |
---|---|---|
= | x = 5 | Assign 5 to x |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
//= | x //= 2 | Floor division |
%= | x %= 2 | Modulo assign |
**= | x **= 2 | Power assign |
Example:
x = 10
x += 5
print(x) # 15
x *= 2
print(x) # 30
4. Comparison Operators
Used to compare two values. Returns True
or False
.
Operator | Meaning | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater or equal | x >= y |
<= | Less or equal | x <= y |
Example:
x = 10
y = 5
print(x > y) # True
print(x == y) # False
print(x != y) # True
5. Logical Operators
Used to combine multiple Boolean expressions.
Operator | Description | Example |
---|---|---|
and | True if both are True | x > 5 and y < 10 |
or | True if at least one True | x > 5 or y > 10 |
not | Reverses the result | not(x > 5) |
Example:
x = 10
y = 5
print(x > 5 and y < 10) # True
print(x > 5 or y > 10) # True
print(not(x > 5)) # False
6. Identity Operators
Used to compare memory locations of objects.
Operator | Meaning | Example |
---|---|---|
is | True if same object | x is y |
is not | True if different objects | x is not y |
Example:
a = [1, 2]
b = a
c = [1, 2]
print(a is b) # True
print(a is c) # False
print(a is not c) # True
7. Membership Operators
Used to check if a value is part of a sequence (string, list, tuple, etc.)
Operator | Meaning | Example |
---|---|---|
in | True if value exists | "a" in "abc" |
not in | True if not exists | 4 not in [1,2,3] |
Example:
print("H" in "Hello") # True
print(3 not in [1, 2, 4]) # True
8. Bitwise Operators
Used to compare binary numbers (advanced).
Operator | Name | Example |
---|---|---|
& | AND | a & b |
` | ` | OR |
^ | XOR | a ^ b |
~ | NOT | ~a |
<< | Left Shift | a << 2 |
>> | Right Shift | a >> 2 |
a = 5 # 0101
b = 3 # 0011
print(a & b) # 1
print(a | b) # 7
print(a ^ b) # 6
print(~a) # -6
✅ Summary Table
Category | Operators |
---|---|
Arithmetic | + , - , * , / , % , // , ** |
Assignment | = , += , -= , *= , /= , etc. |
Comparison | == , != , > , < , >= , <= |
Logical | and , or , not |
Identity | is , is not |
Membership | in , not in |
Bitwise | & , ` |