Introduction
In Python, operators are fundamental building blocks used to perform operations on variables and values. Whether you're building a calculator, writing conditions, or designing logic — operators are everywhere.
Key Insight:
Operators are the backbone of decision-making and computation in programming.
What is an Operator?
An operator is a symbol that performs an operation on operands (values or variables).
a = 10
b = 5
print(a + b) # Output: 15
-
+→ Operator -
a, b→ Operands
Why Operators Matter
Operators are used in:
- Calculations →
price + tax - Conditions →
age > 18 - Logic →
is_logged_in and is_verified - Control flow → loops, decisions
Types of Operators in Python
1. Arithmetic Operators
Used for mathematical operations.
print(10 + 5) # 15
print(10 - 5) # 5
print(10 * 5) # 50
print(10 / 5) # 2.0
print(10 // 3) # 3
print(10 % 3) # 1
print(2 ** 3) # 8
Notes:
-
/→ always returns float -
//→ floor division -
%→ useful for even/odd checks
2. Assignment Operators
Used to assign and update values.
x = 10
x += 5 # 15
x -= 2 # 13
x *= 2 # 26
x /= 2 # 13.0
Notes:
- Short-hand operations improve readability
- Common in loops and updates
3. Comparison Operators
Used to compare values → returns Boolean
print(10 == 10) # True
print(10 != 5) # True
print(10 > 5) # True
print(10 < 5) # False
Notes:
-
Always returns
TrueorFalse -
Used in conditions (
if, loops)
4. Logical Operators
Combine multiple conditions
print(True and False) # False
print(True or False) # True
print(not True) # False
Notes:
-
and→ all conditions must be True -
or→ at least one True -
not→ reverses result
5. Identity Operators
Check if two variables refer to the same object
a = [1, 2]
b = a
c = [1, 2]
print(a is b) # True
print(a is c) # False
print(a == c) # True
Notes:
-
is→ checks memory location -
==→ checks value
6. Membership Operators
Check if a value exists in a sequence
print("a" in "apple") # True
print(10 in [1, 2, 3]) # False
print(1 in (1, 2, 3)) # True
Notes:
- Works with strings, lists, tuples, sets
Operator Precedence
Precedence determines which operator executes first.
print(2 + 3 * 4) # 14
Multiplication happens before addition
Associativity
Associativity determines evaluation order when precedence is same.
Left to Right
print(10 - 5 - 2) # 3
Right to Left
print(2 ** 3 ** 2) # 512
Precedence + Associativity Table
| Priority | Operators | Associativity |
|---|---|---|
| 1 | () | Left → Right |
| 2 | ** | Right → Left |
| 3 | +x, -x, not | Right → Left |
| 4 | * / // % | Left → Right |
| 5 | + - | Left → Right |
| 6 | Comparisons | Left → Right |
| 7 | and | Left → Right |
| 8 | or | Left → Right |
| 9 | Assignment | Right → Left |
Important Notes (Exam / Interview Level)
-
True = 1,False = 0 -
andreturns first falsy value -
orreturns first truthy value -
is≠== - Exponent is right-associative
- Division always returns float
Practice Questions (NEW – Not Repeated)
Q1
print(8 + 2 * 5 - 3)
Q2
print(20 // 3 + 2 ** 2)
Q3
print(5 > 3 and 8 < 5 or 10 > 2)
Q4
print(not (4 == 4 and 2 > 3))
Q5
x = 10
x -= 3 * 2
print(x)
Q6
print(6 + 3 * 2 ** 2)
Q7
print("p" in "python" and 5 not in [1,2,3])
Q8
a = [1,2,3]
b = a
c = a[:]
print(a is c)
Q9
print(0 or 5 and 10)
Q10 (Advanced)
print(2 ** 2 ** 3 + 1)
.png)

0 Comments:
Post a Comment