Python provides a variety of operators that perform operations on variables and values. Operators are grouped based on their functionality.
1. Arithmetic Operators
Used to perform mathematical operations.
Operator | Description | Example | Result |
+ | Addition | 5 + 3 | 8 |
– | Subtraction | 5 – 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division (float) | 5 / 3 | 1.666… |
// | Floor Division | 5 // 3 | 1 |
% | Modulus (Remainder) | 5 % 3 | 2 |
** | Exponentiation | 5 ** 3 | 125 |
2. Comparison Operators
Compare two values and return a boolean result (True or False).
Operator | Description | Example | Result |
== | Equal to | 5 == 3 | FALSE |
!= | Not equal to | 5 != 3 | TRUE |
> | Greater than | 5 > 3 | TRUE |
< | Less than | 5 < 3 | FALSE |
>= | Greater than or equal to | 5 >= 3 | TRUE |
<= | Less than or equal to | 5 <= 3 | FALSE |
3. Logical Operators
Used to combine conditional statements.
Operator | Description | Example | Result |
and | True if both conditions are True | True and False | FALSE |
or | True if at least one condition is True | True or False | TRUE |
not | Reverses the condition | not True | FALSE |
4. Assignment Operators
Used to assign values to variables.
Operator | Description | Example | Equivalent To |
= | Assign | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 3 | x = x – 3 |
*= | Multiply and assign | x *= 3 | x = x * 3 |
/= | Divide and assign | x /= 3 | x = x / 3 |
//= | Floor divide and assign | x //= 3 | x = x // 3 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
**= | Exponentiate and assign | x **= 3 | x = x ** 3 |
5. Bitwise Operators
Used for binary operations.
Operator | Description | Example | Result |
& | AND | 5 & 3 | 1 |
` | ` | OR | `5 |
^ | XOR | 5 ^ 3 | 6 |
~ | NOT (invert bits) | ~5 | -6 |
<< | Left shift | 5 << 1 | 10 |
>> | Right shift | 5 >> 1 | 2 |
6. Membership Operators
Test if a value is in a sequence (like a list, string, or tuple).
Operator | Description | Example | Result |
in | True if value is in sequence | a’ in ‘apple’ | TRUE |
not in | True if value is not in sequence | b’ not in ‘apple’ | TRUE |
7. Identity Operators
Test whether two variables point to the same object in memory.
Operator | Description | Example | Result |
is | True if they are the same object | x is y |
True (if x and y are identical)
|
is not | True if they are not the same object | x is not y |
True (if x and y are different)
|
8. Special Operators
- Ternary (Conditional) OperatorA one-line if-else expression:
x = 10 result = "Even" if x % 2 == 0 else "Odd" print(result) # Output: Even
- Chained ComparisonCombine multiple comparisons in a single line:
x = 5 print(1 < x < 10) # Output: True
Operator Precedence
Operators have a defined order of evaluation. For example:
x = 3 + 5 * 2 # Multiplication happens first
print(x) # Output: 13
Precedence (Highest to Lowest) | Operators |
1 | ** |
2 | *, /, //, % |
3 | #ERROR! |
4 | Comparison (==, !=, >, <, >=, <=) |
5 | not |
6 | and |
7 | or |