Answer: Python has arithmetic, assignment, comparison (relational), logical, identity, membership and bitwise operators.
1. Arithmetic operators (take x = 13, y = 4 where useful)
| Operator | Meaning | Example | Result |
|---|
| + | Addition | 3 + 5 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 2 * 4 | 8 |
| / | Division (always gives a float) | 13 / 4 | 3.25 |
| // | Floor division (rounds down) | 15 // 2 | 7 (and -15 // 2 gives -8) |
| % | Modulus (remainder) | 5 % 2 | 1 |
| ** | Exponent (power) | 2 ** 5 | 32 |
2. Assignment operators (start each with x = 5)
| Operator | Same as | Result |
|---|
| x = 5 | assign 5 to x | 5 |
| x += 3 | x = x + 3 | 8 |
| x -= 3 | x = x - 3 | 2 |
| x *= 3 | x = x * 3 | 15 |
| x /= 3 | x = x / 3 | 1.6666666666666667 |
| x %= 3 | x = x % 3 | 2 |
| x //= 3 | x = x // 3 | 1 |
| x **= 2 | x = x ** 2 | 25 |
3. Comparison (relational) operators (x = 5, y = 3); each gives True or False
| Operator | Meaning | Example | Result |
|---|
| == | Equal to | x == y | False |
| != | Not equal to | x != y | True |
| > | Greater than | x > y | True |
| < | Less than | x < y | False |
| >= | Greater than or equal to | x >= y | True |
| <= | Less than or equal to | x <= y | False |
4. Logical operators (x = 5)
| Operator | Meaning | Example | Result |
|---|
| and | True if both conditions are true | x > 3 and x < 10 | True |
| or | True if at least one condition is true | x > 6 or x < 10 | True |
| not | Reverses the result | not (x > 10) | True |
5. Identity operators: check whether two names refer to the same object in memory (use == to compare values). With a = [1, 2, 3]; b = a; c = [1, 2, 3]:
- a is b gives True (same list object)
- a is c gives False (equal values but two different lists), while a == c gives True
- a is not c gives True
6. Membership operators (s = "Hello how are you ?")
- in: "you" in s gives True
- not in: "Hi" not in s gives True
7. Bitwise operators (a = 12 = 1100, b = 10 = 1010 in binary)
| Operator | Meaning | Example | Result |
|---|
| & | AND: bit is 1 if both bits are 1 | a & b | 8 (1000) |
| | | OR: bit is 1 if either bit is 1 | a | b | 14 (1110) |
| ^ | XOR: bit is 1 if only one of the bits is 1 | a ^ b | 6 (0110) |
| ~ | NOT: inverts all bits, ~a = -(a+1) | ~a | -13 |
| >> | Right shift | a >> 2 | 3 |
| << | Left shift (fills zeros from the right) | a << 2 | 48 |
Checked in Python 3
print(3 + 5, 5 - 3, 2 * 4, 13 / 4, 15 // 2, -15 // 2, 5 % 2, 2 ** 5)
x = 5; x += 3; print(x)
x = 5; x -= 3; print(x)
x = 5; x *= 3; print(x)
x = 5; x /= 3; print(x)
x = 5; x %= 3; print(x)
x = 5; x //= 3; print(x)
x = 5; x **= 2; print(x)
x, y = 5, 3
print(x == y, x != y, x > y, x < y, x >= y, x <= y)
x = 5
print(x > 3 and x < 10, x > 6 or x < 10, not (x > 10))
a = [1, 2, 3]; b = a; c = [1, 2, 3]
print(a is b, a is c, a == c, a is not c)
s = "Hello how are you ?"
print("you" in s, "Hi" not in s)
a, b = 12, 10
print(a & b, a | b, a ^ b, ~a, a >> 2, a << 2)
8 2 8 3.25 7 -8 1 32
8
2
15
1.6666666666666667
2
1
25
False True True False True False
True True True
True False True True
True True
8 14 6 -13 3 48