Operators in Python are special symbols or keywords used to perform operations on variables and values. Whether you're doing math, making comparisons, or working with conditions, operators are everywhere in Python.
In this article, you'll learn:
-
What operators are
-
Types of Python operators
-
Detailed usage with examples
-
Tips and common mistakes
What Are Operators?
Operators are used to perform operations on variables and values.
Example:
x = 10
y = 5
print(x + y) # ➡️ 15 (here, `+` is an operator)
Types of Python Operators
Python supports several types of operators:
-
Arithmetic Operators
-
Assignment Operators
-
Comparison Operators
-
Logical Operators
-
Identity Operators
-
Membership Operators
-
Bitwise Operators
Let’s go through each of them step by step.
1️⃣ Arithmetic Operators
Used for basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 3 + 2 |
5 |
- |
Subtraction | 5 - 2 |
3 |
* |
Multiplication | 3 * 2 |
6 |
/ |
Division | 6 / 2 |
3.0 |
// |
Floor Division | 7 // 2 |
3 |
% |
Modulus (remainder) | 7 % 2 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
2️⃣ Assignment Operators
Used to assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 4 |
x = x * 4 |
/= |
x /= 2 |
x = x / 2 |
//= |
x //= 2 |
x = x // 2 |
%= |
x %= 3 |
x = x % 3 |
**= |
x **= 2 |
x = x ** 2 |
3️⃣ Comparison (Relational) Operators
Used to compare values; always return True
or False
.
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 5 > 2 |
True |
< |
Less than | 5 < 10 |
True |
>= |
Greater or equal | 5 >= 5 |
True |
<= |
Less or equal | 3 <= 4 |
True |
4️⃣ Logical Operators
Used to combine conditional statements.
Operator | Description | Example | Result |
---|---|---|---|
and |
True if both true | True and False |
False |
or |
True if one is true | True or False |
True |
not |
Inverts result | not True |
False |
a = 10
b = 5
if a > 5 and b < 10:
print("Both conditions are True")
5️⃣ Identity Operators
Used to check if two variables refer to the same object (not just equal values).
Operator | Description | Example | Result |
---|---|---|---|
is |
Same identity | x is y |
True/False |
is not |
Not same identity | x is not y |
True/False |
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # True
print(x is z) # False (even if values are the same)
6️⃣ Membership Operators
Used to check if a value exists in a sequence.
Operator | Description | Example | Result |
---|---|---|---|
in |
Value in sequence | 'a' in 'apple' |
True |
not in |
Value not in sequence | 3 not in [1, 2] |
True |
colors = ['red', 'green', 'blue']
print('red' in colors) # True
7️⃣ Bitwise Operators
Operate on binary numbers.
Operator | Description | Example | Result (in binary) |
---|---|---|---|
& |
AND | 5 & 3 |
0001 → 1 |
` | ` | OR | `5 |
^ |
XOR | 5 ^ 3 |
0110 → 6 |
~ |
NOT | ~5 |
-(5+1) → -6 |
<< |
Left Shift | 5 << 1 |
1010 → 10 |
>> |
Right Shift | 5 >> 1 |
0010 → 2 |
Example: Calculator Program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(f"Add: {a + b}")
print(f"Subtract: {a - b}")
print(f"Multiply: {a * b}")
print(f"Divide: {a / b}")
Tips
-
Use
//
if you want an integer result from division. -
Use parentheses
()
to control the order of operations. -
Don’t confuse
is
with==
:is
checks identity, not value.
⚠️ Common Pitfalls
Mistake | Problem | Correct Way |
---|---|---|
5 = x |
Invalid syntax | x = 5 (assignment is = ) |
Using is for value comparison |
x is 5 may not work |
Use x == 5 |
a and b or c confusion |
Order matters | Use parentheses |
float // int result |
Floor division truncates decimals | Use / if you want float |
Summary Table
Operator Type | Examples |
---|---|
Arithmetic | + , - , * , / , // , % , ** |
Assignment | = , += , -= , etc. |
Comparison | == , != , > , < , >= , <= |
Logical | and , or , not |
Identity | is , is not |
Membership | in , not in |
Bitwise | & , ` |
What's Next?
After learning operators, you’re ready to explore:
-
Control structures like
if
,elif
,else
-
Loops (
for
,while
) -
Functions and data structures