DEV Community

Cover image for Operators in Python Explained with Examples
shalini
shalini

Posted on

Operators in Python Explained with Examples

When you start learning Python, one of the first concepts you encounter is operators.

At first, they look simple — symbols like +, -, *. But in reality, operators are the foundation of programming logic.

Without operators, your program cannot:

✓ Perform calculations
✓ Compare values
✓ Make decisions
✓ Build real-world logic

Whether you're a beginner, student, or developer, understanding Python operators is a must.

What are Operators in Python?

Operators in Python are special symbols used to perform operations on variables and values.

Example:

a = 10
b = 5
Enter fullscreen mode Exit fullscreen mode

print(a + b) # Output: 15

Here:

✓ + is an operator
✓ It performs addition

Why Operators are Important

Operators are used in almost every program you write.

They help you:

✓ Perform mathematical operations
✓ Build if conditions
✓ Create loops and logic
✓ Solve real-world problems
✓ Answer interview questions

Without operators, programming is incomplete.

Types of Operators in Python

Let’s understand each type step by step.

1. Arithmetic Operators

Used for mathematical calculations.

✓ + → Addition
✓ - → Subtraction
✓ * → Multiplication
✓ / → Division
✓ % → Modulus
✓ // → Floor Division
✓ ** → Power
Enter fullscreen mode Exit fullscreen mode

Example:

a = 10
b = 3

print(a + b)   # 13
print(a - b)   # 7
print(a * b)   # 30
print(a / b)   # 3.33
print(a % b)   # 1
print(a // b)  # 3
print(a ** b)  # 1000
Enter fullscreen mode Exit fullscreen mode

2. Comparison Operators

Used to compare values and return True or False.

✓ ==
✓ !=
✓ >
✓ <
✓ >=
✓ <=

Example:

a = 10
b = 20

print(a == b)  # False
print(a != b)  # True
print(a > b)   # False
print(a < b)   # True
Enter fullscreen mode Exit fullscreen mode

3. Logical Operators

Used to combine conditions.

✓ and
✓ or
✓ not
Enter fullscreen mode Exit fullscreen mode

Example:

x = True
y = False

print(x and y)  # False
print(x or y)   # True
print(not x)    # False
Enter fullscreen mode Exit fullscreen mode

4. Assignment Operators

Used to assign values to variables.

✓ =
✓ +=
✓ -=
✓ *=
✓ /=
Enter fullscreen mode Exit fullscreen mode

Example:

a = 10
a += 5
print(a)  # 15
Enter fullscreen mode Exit fullscreen mode

5. Bitwise Operators

Used for operations on binary numbers.

✓ &
✓ |
✓ ^
✓ ~
✓ <<
✓ >>
Enter fullscreen mode Exit fullscreen mode

Example:

a = 5
b = 3

print(a & b)  # 1
print(a | b)  # 7
Enter fullscreen mode Exit fullscreen mode

6. Membership Operators

Used to check if a value exists.

✓ in
✓ not in

Example:

list1 = [1, 2, 3]

print(2 in list1)      # True
print(5 not in list1)  # True
Enter fullscreen mode Exit fullscreen mode

7. Identity Operators

Used to compare memory locations.

✓ is
✓ is not

Example:

a = [1, 2, 3]
b = a

print(a is b)  # True
Enter fullscreen mode Exit fullscreen mode

Key Concepts to Remember

✓ Operators work on operands (values/variables)
✓ Some return Boolean values
✓ Operator precedence matters
✓ Python follows PEMDAS rule

Real-World Use Cases

Operators are used everywhere:

✓ Banking apps → calculations
✓ E-commerce → price comparison
✓ Login systems → conditions
✓ Data analysis → filtering
✓ AI/ML → mathematical operations

** Advantages**

✓ Simple and easy to use
✓ Improves readability
✓ Supports complex logic
✓ Essential for programming

Disadvantages

✓ Bitwise operators can be confusing
✓ Precedence can cause mistakes
✓ Wrong usage leads to bugs

Real-Life Example

age = 18

if age >= 18:
    print("Eligible to vote")
else:
    print("Not eligible")

Enter fullscreen mode Exit fullscreen mode

Here:

✓ >= is a comparison operator
✓ Used in real-world logic
Enter fullscreen mode Exit fullscreen mode

Tools to Practice

✓ Python IDLE
✓ VS Code
✓ PyCharm
✓ Jupyter Notebook
✓ Google Colab

Common Mistakes

✓ Confusing = and ==
✓ Ignoring operator precedence
✓ Misusing logical operators
✓ Not using parentheses
✓ Lack of practice

Interview Questions

** What are operators?**

✓ Symbols used to perform operations

Difference between = and ==?

✓ = → Assignment
✓ == → Comparison

What are logical operators?

✓ and, or, not

What is precedence?

✓ Order of execution

** What is 'is' operator?**

✓ Checks memory reference

FAQs

Types of operators?

✓ Arithmetic, Comparison, Logical, Assignment, Bitwise, Membership, Identity

Power operator?

✓ **

Modulus?

✓ % gives remainder

Floor division?

✓ // removes decimal

Important for beginners?

✓ Yes, very important

Final Thoughts

Understanding operators in Python is one of the most important first steps in programming.

Once you master them:

✓ You can build strong logic
✓ Solve real-world problems
✓ Perform well in interviews

Start practicing today and improve step by step.

Top comments (0)