⚙️ Python Operators – Arithmetic, Logical, and More Explained | TechTown.in

In Python, operators are the tools we use to perform actions on variables and values — from simple math to complex logical checks.

In this guide, we’ll explore all types of Python operators, including arithmetic, comparison, logical, assignment, and more. With practical examples and simple explanations, you’ll understand when and how to use each operator in your code.


🔹 What Are Operators in Python?

An operator is a symbol that tells Python to perform a specific operation on one or more values or variables.

Python has the following operator categories:

CategoryExample Symbols
Arithmetic Operators+, -, *, /, %
Assignment Operators=, +=, -=
Comparison Operators==, !=, <, >
Logical Operatorsand, or, not
Identity Operatorsis, is not
Membership Operatorsin, not in
Bitwise Operators&, `

➕ Arithmetic Operators (Used in Math)

x = 10
y = 3

print(x + y)  # 13 → Addition
print(x - y)  # 7  → Subtraction
print(x * y)  # 30 → Multiplication
print(x / y)  # 3.33 → Division
print(x % y)  # 1  → Modulus (remainder)
print(x ** y) # 1000 → Exponent (power)
print(x // y) # 3  → Floor division (rounded down)

📝 Assignment Operators (Set or Update Values)

x = 5      # Simple assignment
x += 3     # x = x + 3 → 8
x -= 1     # x = x - 1 → 7
x *= 2     # x = x * 2 → 14
x /= 2     # x = x / 2 → 7.0

Other operators: %=, //=, **=, etc.


🔁 Comparison Operators (True/False Output)

Used in conditions, loops, and validation.

a = 10
b = 20

print(a == b)  # False
print(a != b)  # True
print(a < b)   # True
print(a >= b)  # False

These return True or False.


⚙️ Logical Operators (And/Or/Not)

Used to combine multiple conditions.

x = 5
print(x > 3 and x < 10)  # True
print(x > 10 or x < 2)   # False
print(not(x > 3))        # False

🆔 Identity Operators (Object Identity)

Check whether two variables point to the same memory location.

x = ["Python"]
y = x
z = ["Python"]

print(x is y)       # True
print(x is z)       # False (same value, different objects)
print(x is not z)   # True

🧩 Membership Operators (In or Not In)

Check if a value exists in a list, string, or other iterable.

fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)     # True
print("grape" not in fruits) # True

Also useful in if conditions and filters.


🧠 Bitwise Operators (Advanced)

Work at the binary level, often used in performance-critical apps.

a = 5      # 0101
b = 3      # 0011

print(a & b)  # 1  (0101 & 0011 → 0001)
print(a | b)  # 7  (0101 | 0011 → 0111)
print(a ^ b)  # 6  (XOR)
print(~a)     # -6 (NOT)
print(a << 1) # 10 (Left shift)
print(a >> 1) # 2  (Right shift)

📌 Summary: Python Operator Types

Operator TypeUseExample
ArithmeticMath operationsx + y
AssignmentAssign/change variablex += 2
ComparisonTrue/False testsx > y
LogicalCombine conditionsx > 5 and x < 10
IdentityCompare memory locationx is y
MembershipCheck item in collection"a" in "apple"
BitwiseBinary-level operationsx & y

✅ Quick Tips

  • ✅ Use and, or, not to build complex conditions
  • ✅ Use is and in for clearer, more Pythonic code
  • ✅ Know the difference between == (equal value) and is (same object)
  • ✅ Use parentheses to group expressions clearly

🏁 Final Thoughts

Understanding Python operators is essential for writing efficient and clean code. Whether you’re doing calculations, filtering data, or writing conditions — operators help you make your logic work smoothly.

Practice using them in real-world projects, and you’ll soon be thinking like a true Pythonista.


📘 Continue exploring Python fundamentals at TechTown.in