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 (:=).
Perform standard mathematical calculations:
| Operator | Name | Example Code | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Float Division | 7 / 2 | 3.5 |
// | Floor Division | 7 // 2 | 3 |
% | Modulus (Remainder) | 17 % 5 | 2 |
** | Exponentiation (Power) | 2 ** 8 | 256 |
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)
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
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)
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
:=)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.")
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.
-7 / 2 = -3.5. The greatest integer less than or equal to -3.5 is -4.
__add__ for + and __eq__ for ==.