Python Operators: Complete Guide with Precedence & Practical Examples

An operator is a special symbol or keyword that carries out mathematical computations, logical evaluations, or data manipulations on operands (variables and literals). Python includes standard arithmetic and comparison operations alongside powerful Python-exclusive operators such as identity checks (is), membership testing (in), and the assignment expression Walrus operator (:=).


The 7 Categories of Python Operators

1. Arithmetic Operators

Perform standard mathematical calculations:

Operator Name Example Code Result
+Addition10 + 515
-Subtraction10 - 46
*Multiplication6 * 742
/Float Division7 / 23.5
//Floor Division7 // 23
%Modulus (Remainder)17 % 52
**Exponentiation (Power)2 ** 8256

2. Comparison (Relational) Operators

Compare two values and return a Boolean result (True or False):

x, y = 15, 20
print(x == y)   # False (Equality)
print(x != y)   # True  (Not Equal)
print(x < y)    # True  (Less Than)
print(x >= 15)  # True  (Greater than or equal)

# Chained Comparisons in Python (unique feature!):
score = 85
print(80 <= score < 90)  # True (Equivalent to: score >= 80 and score < 90)

3. Logical Operators (and, or, not)

Combine conditional statements using natural English keywords:

attendance = 82
marks = 74

# 'and' requires both conditions True:
is_eligible = (attendance >= 75) and (marks >= 40)  # True

# 'not' reverses truth value:
print(not is_eligible)  # False

4. Identity Operators (is vs ==)

This is one of the most frequently asked viva and interview questions:

list_a = [1, 2, 3]
list_b = [1, 2, 3]
list_c = list_a

print(list_a == list_b)  # True  (Their contents are identical)
print(list_a is list_b)  # False (Different objects at different memory addresses!)
print(list_a is list_c)  # True  (Both point to the exact same memory ID)

5. Membership Operators (in and not in)

Quickly test if an element exists inside any sequence (string, list, tuple, dictionary, set):

languages = ["Python", "JavaScript", "C++", "Java"]

print("Python" in languages)      # True
print("Ruby" not in languages)     # True

# Substring check in text:
headline = "Learn Python for BCA semester exams"
print("BCA" in headline)          # True

6. The Modern Walrus Operator (:=)

Introduced in Python 3.8, the assignment expression operator assigns a value to a variable inside an expression, eliminating repetitive calls:

# Walrus operator example:
user_input = "Python Programming"

if (length := len(user_input)) > 10:
    print(f"Text is too long! Length is {length} characters.")

Operator Precedence (Order of Evaluation)

When multiple operators appear in a single expression, Python evaluates them in strict precedence order (use parentheses () to force priority):

1. Parentheses () → 2. Exponentiation ** → 3. Unary signs +x, -x → 4. Multiplication, Division *, /, //, % → 5. Addition, Subtraction +, - → 6. Comparisons ==, !=, <, > → 7. Membership & Identity in, is → 8. Logical not, and, or.


Frequently Asked Questions (FAQ)

Why does -7 // 2 equal -4 instead of -3?

Python uses floor division, which rounds down towards negative infinity. -7 / 2 = -3.5. The greatest integer less than or equal to -3.5 is -4.

Can we overload operators for our own custom classes?

Yes! Python allows full Operator Overloading using special dunder methods like __add__ for + and __eq__ for ==.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!