Operators are one of the most fundamental concepts in Python. They allow you to perform calculations, compare values, make decisions, and manipulate data efficiently. Whether you're building a calculator, analyzing data with Pandas, or developing AI applications, you'll use operators in almost every Python program.
In this blog, we'll explore all the major types of Python operators with syntax, examples, and real-world use cases.
An operator is a special symbol or keyword that performs an operation on one or more operands (values or variables).
Example
a = 10
b = 5
print(a + b)
Output
15
Here:
+
is the operator.a
and b
are operands.Python provides the following categories of operators:
| Operator Type | Purpose |
|---|---|
| Arithmetic Operators | Mathematical calculations |
| Comparison Operators | Compare values |
| Assignment Operators | Assign values to variables |
| Logical Operators | Combine conditions |
| Bitwise Operators | Binary operations |
| Membership Operators | Check if a value exists |
| Identity Operators | Compare object identity |
Arithmetic operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| // | Floor Division | a // b |
| % | Modulus (Remainder) | a % b |
| ** | Exponent (Power) | a ** b |
Example:
a = 15
b = 4
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
Output
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Floor Division: 3
Modulus: 3
Exponent: 50625
Comparison operators compare two values and always return either True or False.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Example
a = 20
b = 15
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
Output
False
True
True
False
True
False
Assignment operators assign values to variables.
| Operator | Example | Same As |
|---|---|---|
| = | x = 5 | Assign |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 3 | x = x // 3 |
| **= | x **= 3 | x = x ** 3 |
Example
x = 10
x += 5
print(x)
x *= 2
print(x)
x -= 4
print(x)
Output
15
30
26
Logical operators combine multiple conditions.
| Operator | Meaning |
|---|---|
| and | Both conditions must be True |
| or | At least one condition is True |
| not | Reverses the result |
Example
age = 22
salary = 60000
print(age > 18 and salary > 50000)
print(age < 18 or salary > 50000)
print(not(age > 18))
Output
True
True
False
Bitwise operators work on binary numbers.
| Operator | Description |
|---|---|
| & | AND |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
Example
a = 5
b = 3
print(a & b)
print(a | b)
print(a ^ b)
Output
1
7
6
These operators are commonly used in low-level programming, networking, cryptography, and performance optimization.
Membership operators check whether a value exists in a sequence.
| Operator | Meaning |
|---|---|
| in | Exists |
| not in | Doesn't exist |
Example
fruits = ["Apple", "Banana", "Mango"]
print("Apple" in fruits)
print("Orange" in fruits)
print("Orange" not in fruits)
Output
True
False
True
Identity operators compare whether two variables refer to the same object in memory.
| Operator | Meaning |
|---|---|
| is | Same object |
| is not | Different objects |
Example
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)
print(a is c)
print(a == c)
Output
True
False
True
Notice the difference:
==
compares values.is
compares object identity.Python evaluates operators according to precedence.
Order (highest to lowest):
()
**
*
, /
, //
, %
+
, -
not
and
or
Example
result = 5 + 3 * 2
print(result)
Output
11
Python first performs multiplication and then addition.
=
instead of ==
Incorrect
if a = 5:
Correct
if a == 5:
is
with ==
a = [1]
b = [1]
print(a == b)
print(a is b)
Output
True
False
print(5 + 2 * 3)
Output
11
Use parentheses for clarity.
print((5 + 2) * 3)
Output
21
Python operators are the building blocks of programming. Understanding them helps you write cleaner, faster, and more efficient code.
Here's a quick recap:
True
or False
.Mastering these operators is essential because they appear in almost every Python program—from simple scripts to advanced applications in data science, web development, automation, and artificial intelligence.
Happy Coding! 🚀