Operators are special symbols that perform operations on values and variables.
Used to perform mathematical operations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 3 |
13 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 10 * 3 |
30 |
/ |
Division | 10 / 3 |
3.333... |
// |
Floor Division | 10 // 3 |
3 |
% |
Modulus | 10 % 3 |
1 |
** |
Exponentiation | 10 ** 2 |
100 |
a = 10
b = 3
print(a + b) # 13
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
Used to assign values to variables.
| Operator | Example | Same As |
|---|---|---|
= |
x = 10 |
x = 10 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
//= |
x //= 5 |
x = x // 5 |
%= |
x %= 5 |
x = x % 5 |
**= |
x **= 5 |
x = x ** 5 |
x = 10
x += 5 # x = 15
x -= 3 # x = 12
x *= 2 # x = 24
x //= 3 # x = 8
x **= 2 # x = 64
Used to compare two values.
| Operator | Name | Example | Result |
|---|---|---|---|
== |
Equal | 5 == 5 |
True |
!= |
Not Equal | 5 != 3 |
True |
> |
Greater Than | 5 > 3 |
True |
< |
Less Than | 5 < 3 |
False |
>= |
Greater Than or Equal | 5 >= 5 |
True |
<= |
Less Than or Equal | 5 <= 3 |
False |
a = 10
b = 20
print(a == b) # False
print(a != b) # True
print(a < b) # True
print(a >= 10) # True
Used to combine conditional statements.
| Operator | Name | Example | Result |
|---|---|---|---|
and |
Returns True if both are True | (5 > 3) and (2 < 4) |
True |
or |
Returns True if at least one is True | (5 > 3) or (2 > 4) |
True |
not |
Reverses the result | not(5 > 3) |
False |
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
Used to perform bit-level operations.
| Operator | Name | Example (x = 5, y = 3) | Result |
|---|---|---|---|
& |
AND | 5 & 3 (0101 & 0011) |
1 |
| |
OR | 5 | 3 (0101 | 0011) |
7 |
^ |
XOR | 5 ^ 3 (0101 ^ 0011) |
6 |
~ |
NOT | ~5 (~0101) |
-6 |
<< |
Left Shift | 5 << 1 (0101 << 1) |
10 |
>> |
Right Shift | 5 >> 1 (0101 >> 1) |
2 |
a = 5 # 0101
b = 3 # 0011
print(a & b) # 1
print(a | b) # 7
print(~a) # -6
print(a << 1) # 10
Used to test membership in a sequence.
| Operator | Name | Example | Result |
|---|---|---|---|
in |
Returns True if value is found | 'a' in 'apple' |
True |
not in |
Returns True if value is not found | 'z' not in 'apple' |
True |
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits) # True
print('orange' in fruits) # False
print('grape' not in fruits)# True
Used to compare the memory location of objects.
| Operator | Name | Example | Result |
|---|---|---|---|
is |
Returns True if both variables point to the same object | a is b |
True |
is not |
Returns True if both variables point to different objects | a is not b |
False |
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is c) # False
print(a is not c) # True
Used to evaluate a condition in a single line.
value_if_true if condition else value_if_false
age = 18
status = "Adult" if age >= 18 else "Minor"
print(status) # Adult
| Arithmetic | Assignment | Comparison | Logical | Bitwise | Membership | Identity |
|---|---|---|---|---|---|---|
+ - * / //% **
|
= += -= *= /=//= %= **=
|
== != > <>= <=
|
and or not
|
& | ^ ~<< >>
|
in not in
|
is is not
|