
What are Operators?
In Python, operators are special symbols used to perform operations on values and variables. They allow you to perform calculations, compare values, make decisions.
Types of Operators
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Assignment Operators
- Membership Operators
- Identity Operators
- Bitwise Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 10 - 4 = 6 |
* | Multiplication | 4 * 2 = 8 |
/ | Division | 10 / 2 = 5.0 |
% | Modulus (remainder) | 10 % 3 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
// | Floor division | 7 // 2 = 3 |
a = 10
b = 3
print(a + b)
print(a % b)
print(a ** b)
Comparison Operator
These operators compare two values and return a boolean result (True or False).
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
x = 10
y = 5
print(x > y) # True
print(x == y) # False
Logical Operators
Logical operators are used to combine conditional statements.
| Operator | Meaning |
|---|---|
and | True if both conditions are true |
or | True if at least one condition is true |
not | Reverses the condition |
age = 25
has_id = True
print(age > 18 and has_id)
Assignment Operators
Assignment operators assign values to variables.
| Operator | Example | Equivalent To |
|---|---|---|
= | x = 5 | Assign value |
+= | x += 2 | x = x + 2 |
-= | x -= 2 | x = x - 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
x = 10
x += 5
print(x)
Membership Operators
Membership operators test whether a value exists inside a sequence.
| Operator | Description |
|---|---|
in | Returns True if value is found |
not in | Returns True if value is not found |
fruits = ["apple", "banana", "mango"]
print("apple" in fruits)
print("grape" not in fruits)
Identity Operators
Identity operators check if two variables refer to the same object in memory.
| Operator | Meaning |
|---|---|
is | True if both variables refer to same object |
is not | True if they do not |
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is c) # False
Bitwise Operators
Bitwise operators work on binary (bit-level) representations of numbers. Instead of working with whole values, they operate on individual bits (0s and 1s).
| Operator | Name | Description |
|---|---|---|
& | AND | Sets bit to 1 if both bits are 1 |
| | | OR | Sets bit to 1 if either of the bits are 1 |
^ | XOR | Sets bit to 1 if bits are different |
~ | NOT | Inverts all bits |
<< | Left Shift | Shifts bits to the left |
>> | Right Shift | Shifts bits to the right |
Example
a = 5 # 0101
b = 3 # 0011
print(a & b) # Output: 1
Explanation
0101 AND (Both Bits must be 1)
0011
----
0001 → 1
Operator Precedence
Operator precedence is used when multiple operators are used in a single expression. The official Python Doc has a Precedence table which goes from highest precedence (most binding) to lowest precedence (least binding). Operators in same cell have the same precedence