Overview & Precedence
Operators are special symbols used to perform operations on values and variables. Python categorizes operators into 7 distinct groups:
| Order | Category | Primary Use / Function |
|---|---|---|
| 1 | Arithmetic | Perform mathematical calculations |
| 2 | Assignment | Assign or update variable values |
| 3 | Comparison | Relational checks returning True/False |
| 4 | Logical | Combine multiple conditional expressions |
| 5 | Membership | Test if a value exists inside a collection |
| 6 | Identity | Check if two variables reference the same memory object |
| 7 | Bitwise | Perform bit-level operations on integers |
Arithmetic Operators
Arithmetic operators mathematical operations execute karte hain:
| Operator | Name | Example | Description |
|---|---|---|---|
+ | Addition | 4 + 2 → 6 | Adds values together |
- | Subtraction | 4 - 2 → 2 | Subtracts right value from left |
* | Multiplication | 4 * 2 → 8 | Multiplies values |
/ | Division | 4 / 2 → 2.0 | Divides values (returns float) |
// | Floor Division | 4 // 2 → 2 | Divides and rounds down to whole int |
% | Modulus | 4 % 2 → 0 | Returns remainder of division |
** | Exponentiation | 4 ** 2 → 16 | Power calculation (base ** exponent) |
x = 4 y = 2 print(x + y) print(x - y) print(x * y) print(x / y) print(x // y) print(x ** y)
x = 50 y = 30 b = 20 m = 60 print(x - y + b - m)
Comparison Operators
Comparison (Relational) operators compare two values and return boolean True or False.
| Operator | Name | Example | Boolean Output |
|---|---|---|---|
== | Equal | 5 == 5 | True |
!= | Not Equal | 5 != 3 | True |
> | Greater Than | 5 > 2 | True |
< | Less Than | 5 < 2 | False |
>= | Greater Than or Equal | 5 >= 5 | True |
<= | Less Than or Equal | 5 <= 6 | True |
a = 20 b = 20 print(a == b) print(a != b) print(a > b) print(a < b) print(a >= b) print(a <= b)
Assignment Operators
Assignment operators variable ko value assign karne ya existing value ko update karne ke liye use hote hain.
| Operator | Equivalent Expression | Example | Result (if x = 10 initial) |
|---|---|---|---|
= | x = 10 | x = 10 | 10 |
+= | x = x + 5 | x += 5 | 15 |
-= | x = x - 3 | x -= 3 | 7 |
*= | x = x * 2 | x *= 2 | 20 |
/= | x = x / 2 | x /= 2 | 5.0 |
%= | x = x % 3 | x %= 3 | 1 |
//= | x = x // 3 | x //= 3 | 3 |
**= | x = x ** 3 | x **= 3 (initial 2) | 8 |
x = 10 x += 5 print(x) x %= 3 print(x)
Logical Operators
Logical operators combine conditional statements:
- and: Returns True if both conditions are True.
- or: Returns True if at least one condition is True.
- not: Reverse the boolean result (True becomes False, False becomes True).
a = 200
b = 33
c = 50
if a > b and c > a:
print("Both conditions are True")
else:
print("One or both conditions are False")
if a > b or a > c:
print("At least one of the conditions is True")
else:
print("One or both conditions are False")
if not a > b:
print("a is NOT greater than b")
else:
print("a is greater than b")Identity Operators & == vs is
Identity operators check whether two variables refer to the exact same object in computer memory space.
is— Returns True if both variables point to the same object.is not— Returns True if variables point to different objects.
a = [1, 2, 3] b = a print(a is b) c = [1, 2, 3] print(a is not c)
== compares values (content equality).
is compares object identity (memory reference location).
a = [1, 2] b = [1, 2] print(a == b) print(a is b)
Membership Operators
Membership operators check whether a value exists inside a sequence or collection (such as strings, lists, tuples, or sets).
in— Returns True if value is present in collection.not in— Returns True if value is NOT present in collection.
names = ["Gagan", "Rajat", "Lokesh"]
print("Gagan" in names)
print("Ali" not in names)name = "Gagan"
print("G" in name)
print("z" not in name)Bitwise Operators
Bitwise operators work directly on the binary bit representations of integers.
| Operator | Name | Example (a=5 [101], b=3 [011]) | Binary Result | Decimal Output |
|---|---|---|---|---|
& | AND | 5 & 3 | 101 & 011 → 001 | 1 |
| | OR | 5 | 3 | 101 | 011 → 111 | 7 |
^ | XOR | 5 ^ 3 | 101 ^ 011 → 110 | 6 |
~ | NOT | ~5 | -(5 + 1) | -6 |
<< | Left Shift | 5 << 1 | 1010 | 10 |
>> | Right Shift | 5 >> 1 | 010 | 2 |
a = 5 b = 3 print(a & b) print(a | b) print(a ^ b) print(~5) print(5 << 1) print(5 >> 1)