Operators: Arithmetic, Comparison, Logical, Bitwise
অপারেটর — গণিত, তুলনা, যুক্তি, বিট
1. What Is an Operator?
An operator is a symbol (or keyword) that tells Python to perform a specific operation on one or more
values — called operands. Python operators fall into a handful of broad families:
arithmetic (math), comparison (equality/ordering),
logical (and/or/not), bitwise (bit-level manipulation),
assignment, membership (in), and identity (is).
2. Arithmetic Operators
| Op | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | True division (always float) | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Modulus (remainder) | 7 % 2 | 1 |
** | Exponentiation | 2 ** 10 | 1024 |
/ সবসময় float দেয় (7 / 2 == 3.5), আর // floor দেয় (7 // 2 == 3)। C/Java-এর integer division-এর মতো চাইলে // ব্যবহার করুন।
a, b = 17, 5
print(f"{a} + {b} = {a + b}")
print(f"{a} / {b} = {a / b}") # float
print(f"{a} // {b} = {a // b}") # floor
print(f"{a} % {b} = {a % b}") # remainder
print(f"{a} ** {b} = {a ** b}") # power
# Python handles huge integers with no overflow
print(2 ** 100)
3. Comparison Operators & Chaining
Python's comparison operators are standard (==, !=, <,
<=, >, >=) — with one beautiful twist you won't find in C or Java:
comparison chaining.
==, !=, <, <=, >, >= — কিন্তু একটি বিশেষ বৈশিষ্ট্য রয়েছে যা C/Java-তে নেই: comparison chaining।
age = 22
# Traditional way
if age >= 18 and age <= 60:
print("working age")
# Pythonic way — chained
if 18 <= age <= 60:
print("working age (chained)")
# Chains can be longer
x = 5
print(0 < x < 10 < 100)
4. Logical Operators: and, or, not
Python uses English words instead of &&, ||, !. They are
short-circuit — evaluation stops as soon as the result is known. They also return one of
the operands (not always a bool), which enables handy idioms.
# Short-circuit
def expensive():
print("called!")
return True
# `expensive()` never runs — False short-circuits `and`
print(False and expensive())
# Returns an operand, not a bool
name = ""
display = name or "Anonymous"
print(display) # Anonymous
# Truthiness: [], '', 0, None, {} are falsy
for v in [0, 1, "", "x", [], [1], None]:
print(bool(v), v)
5. Bitwise Operators
| Op | Name | Example |
|---|---|---|
& | AND | 0b1100 & 0b1010 == 0b1000 |
| | OR | 0b1100 | 0b1010 == 0b1110 |
^ | XOR | 0b1100 ^ 0b1010 == 0b0110 |
~ | NOT (inverts bits) | ~5 == -6 |
<< | Left shift | 1 << 4 == 16 |
>> | Right shift | 16 >> 2 == 4 |
# Check if a number is a power of two
def is_power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
for n in [1, 2, 3, 4, 8, 15, 16]:
print(n, is_power_of_two(n))
# Swap using XOR (just a trick — rarely Pythonic)
a, b = 5, 9
a ^= b; b ^= a; a ^= b
print(a, b)
6. Assignment, Membership & Identity Operators
- Compound assignment:
+=, -=, *=, /=, //=, %=, **=, &=, |=, ^=, <<=, >>= - Walrus (PEP 572):
:=— assigns and returns in one expression (Python 3.8+) - Membership:
in,not in - Identity:
is,is not
fruits = ["apple", "mango", "jackfruit"]
print("mango" in fruits) # True
print("orange" not in fruits) # True
# Walrus operator (Python 3.8+)
data = [1, 2, 3, 4, 5]
if (n := len(data)) > 3:
print(f"List has {n} items — too many")
7. Precedence — Who Goes First
Operator precedence controls which operation runs first in an expression without parentheses.
The partial order (from highest to lowest): ** → unary -/~ → * / // %
→ + - → shifts → & → ^ → | → comparisons → not
→ and → or.
নিয়ম: সন্দেহ হলে parenthesis ব্যবহার করুন। কয়েকটা অতিরিক্ত bracket দিলে কোডের readability অনেক ভালো হয়।
8. Vocabulary (শব্দভাণ্ডার)
| Term | Meaning | বাংলায় |
|---|---|---|
| Operand | A value an operator acts on. | যে মানের উপর operator কাজ করে। |
| Short-circuit | Stop evaluating as soon as result is known. | ফলাফল জানা হয়ে গেলে evaluation থামিয়ে দেওয়া। |
| Truthy / Falsy | Non-boolean values that act as True/False in bool context. | যে সব non-boolean মান condition-এ True বা False হিসেবে আচরণ করে। |
| Walrus | The := operator that assigns and returns. | := — একই সাথে assign ও return করে। |
| Precedence | Order of operator evaluation. | অপারেটর evaluation-এর ক্রম। |
9. Practice Problems
-
Compute the number of seconds in a year (365 days) using a single expression.এক লাইনের একটি expression দিয়ে এক বছরের (৩৬৫ দিন) মোট সেকেন্ড বের করুন।
✨ Show Answer (উত্তর দেখুন)
ans1.pyprint(365 * 24 * 60 * 60) -
Write a function
is_even(n)using the modulus operator.Modulus operator ব্যবহার করেis_even(n)ফাংশন লিখুন।✨ Show Answer (উত্তর দেখুন)
ans2.pydef is_even(n): return n % 2 == 0 for k in range(6): print(k, is_even(k)) -
Using comparison chaining, check whether a number
xis strictly between 10 and 20.Comparison chaining ব্যবহার করে চেক করুনx10 ও 20-এর মাঝে আছে কি না।✨ Show Answer (উত্তর দেখুন)
ans3.pyx = 15 print(10 < x < 20) -
Explain why
[] or "default"evaluates to"default".ব্যাখ্যা করুন —[] or "default"কেন"default"দেয়।✨ Show Answer (উত্তর দেখুন)
Answer: Empty list
[]is falsy. Theoroperator returns the first truthy operand; since[]is falsy it moves on and returns the second operand"default". This is the Pythonic way to provide fallbacks.খালি list
[]falsy।orপ্রথম truthy operand return করে;[]falsy বলে পরবর্তী operand"default"return হয়। এটি Python-এ fallback দেওয়ার idiomatic উপায়। -
Check whether
16is a power of two using only bitwise operators.শুধুমাত্র bitwise operator ব্যবহার করে162-এর পাওয়ার কি না যাচাই করুন।✨ Show Answer (উত্তর দেখুন)
ans5.pyn = 16 print(n > 0 and (n & (n - 1)) == 0)
Summary — Module 07
Python has a rich operator set. Key points: / always returns float, // is floor division,
comparison operators can be chained (0 < x < 10), and/or short-circuit and return an operand
(not necessarily a bool), and Python's integers are arbitrary-precision so bit tricks scale without overflow.
/ সবসময় float, // floor division, comparison chain করা যায় (0 < x < 10), and/or short-circuit এবং operand return করে, এবং Python integer-এ overflow নেই।