পাঠসূচি
- 1পাইথন পরিচিতি ও ইনস্টলেশন (Python Introduction & Installation)
- 2Python Variables, Datatypes & Type Casting - পাইথন ভ্যারিয়েবল ও টাইপ কাস্টিং
- 3Python Input & Output Explained - পাইথন ইনপুট ও আউটপুট
- 4Python Operators & Expressions - পাইথন অপারেটর ও এক্সপ্রেশন
- 5Python if-elif-else Conditionals - পাইথন কন্ডিশনাল স্টেটমেন্ট
- 6Python Loops: for & while Explained - পাইথন লুপস: for ও while
- 7Python Functions: def, return, arguments - পাইথন ফাংশন ও এরগুমেন্টPlanned
- 8Python List, Tuple & Set Explained - পাইথন লিস্ট, টুপল ও সেটPlanned
- 9Python Dictionary Explained - পাইথন ডিকশনারি ব্যবহারPlanned
- 10Python Strings & String Operations - পাইথন স্ট্রিং ও অপারেশনPlanned
- 11Python File Handling Tutorial - পাইথন ফাইল হ্যান্ডলিংPlanned
- 12Python Error & Exception Handling - পাইথন এক্সেপশন হ্যান্ডলিংPlanned
- 13Python Lambda, map, filter, reduce - পাইথন ল্যাম্বডা ও ফাংশনাল প্রোগ্রামিংPlanned
- 14Python OOP: Class & Object Explained - পাইথন ক্লাস ও অবজেক্টPlanned
- 15Python Inheritance & Polymorphism - পাইথন ইনহেরিটেন্স ও পলিমরফিজমPlanned
- 16Python Modules & Packages Explained - পাইথন মডিউল ও প্যাকেজPlanned
- 17Python time, datetime & calendar - পাইথন টাইম, ডেটটাইম ও ক্যালেন্ডারPlanned
- 18Python JSON Handling - পাইথন JSON ব্যবহারPlanned
- 19Python Regular Expressions - পাইথন রেগুলার এক্সপ্রেশনPlanned
- 20Python Project: Mini Console App - পাইথন প্রজেক্ট: কনসোল অ্যাপ তৈরিPlanned
Python Operators & Expressions - পাইথন অপারেটর ও এক্সপ্রেশন
Contents
1. Operators (অপারেটর)
অপারেটর হল বিশেষ চিহ্ন বা কীওয়ার্ড যা এক বা একাধিক ভ্যালু (অপারেন্ড) এর উপর কাজ করে নতুন ভ্যালু তৈরি করে।
প্রকারভেদ:
- Arithmetic Operators (গাণিতিক অপারেটর)
- Comparison (Relational) Operators (তুলনামূলক অপারেটর)
- Logical Operators (লজিক্যাল অপারেটর)
- Assignment Operators (অ্যাসাইনমেন্ট অপারেটর)
- Bitwise Operators (বিটওয়াইজ অপারেটর) (Advanced)
- Membership Operators (সদস্যতা অপারেটর)
- Identity Operators (পরিচয় অপারেটর)
Arithmetic Operators (গাণিতিক অপারেটর)
গাণিতিক হিসাব নিকাশের জন্য ব্যবহৃত হয়।
| অপারেটর | নাম | উদাহরণ (a=10, b=3) | ফলাফল |
|---|---|---|---|
+ |
Addition | a + b |
13 |
- |
Subtraction | a - b |
7 |
* |
Multiplication | a * b |
30 |
/ |
Division | a / b |
3.333... |
// |
Floor Division | a // b |
3 |
% |
Modulus | a % b |
1 |
** |
Exponentiation | a ** b |
1000 |
a = 10
b = 3
print(f"a + b = {a + b}") # Output: a + b = 13
print(f"a - b = {a - b}") # Output: a - b = 7
print(f"a * b = {a * b}") # Output: a * b = 30
print(f"a / b = {a / b}") # Output: a / b = 3.3333333333333335
print(f"a // b = {a // b}") # Output: a // b = 3 (ভাগফল, দশমিক অংশ বাদ)
print(f"a % b = {a % b}") # Output: a % b = 1 (ভাগশেষ)
print(f"a ** b = {a ** b}") # Output: a ** b = 1000 (10 to the power 3)
# Order of operations (PEMDAS/BODMAS)
result = (10 + 5) * 2 - 3**2 / (4 // 2)
# (15) * 2 - 9 / 2
# 30 - 4.5
# 25.5
print(f"Result of complex expression: {result}") # Output: 25.5
Comparison (Relational) Operators (তুলনামূলক অপারেটর)
দুটি ভ্যালুর মধ্যে তুলনা করে True বা False রিটার্ন করে।
| অপারেটর | নাম | উদাহরণ (x=5, y=10) | ফলাফল |
|---|---|---|---|
== |
Equal to | x == y |
False |
!= |
Not equal to | x != y |
True |
> |
Greater than | x > y |
False |
< |
Less than | x < y |
True |
>= |
Greater than or equal to | x >= 5 |
True |
<= |
Less than or equal to | y <= 10 |
True |
x = 5
y = 10
z = 5
print(f"x == y: {x == y}") # Output: x == y: False
print(f"x == z: {x == z}") # Output: x == z: True
print(f"x != y: {x != y}") # Output: x != y: True
print(f"x > y: {x > y}") # Output: x > y: False
print(f"x < y: {x < y}") # Output: x < y: True
print(f"x >= z: {x >= z}") # Output: x >= z: True
print(f"y <= x: {y <= x}") # Output: y <= x: False
# Strings can also be compared
print(f"'apple' == 'orange': {'apple' == 'orange'}") # Output: False
print(f"'apple' < 'banana': {'apple' < 'banana'}") # Output: True (lexicographical comparison)
Logical Operators (লজিক্যাল অপারেটর)
একাধিক কন্ডিশনাল স্টেটমেন্টকে একত্রিত করতে ব্যবহৃত হয়।
| অপারেটর | নাম | উদাহরণ (a=True, b=False) | ফলাফল |
|---|---|---|---|
and |
Logical AND | a and b |
False |
or |
Logical OR | a or b |
True |
not |
Logical NOT | not a |
False |
p = True
q = False
print(f"p and q: {p and q}") # Output: p and q: False
# True and True -> True
# True and False -> False
# False and True -> False
# False and False -> False
print(f"p or q: {p or q}") # Output: p or q: True
# True or True -> True
# True or False -> True
# False or True -> True
# False or False -> False
print(f"not p: {not p}") # Output: not p: False
print(f"not q: {not q}") # Output: not q: True
# Example with expressions
age = 25
has_license = True
# Eligible to drive if age is 18 or more AND has a license
can_drive = (age >= 18) and has_license
print(f"Can drive: {can_drive}") # Output: Can drive: True
is_weekend = False
has_time = True
# Go to park if it's weekend OR has time
go_to_park = is_weekend or has_time
print(f"Go to park: {go_to_park}") # Output: Go to park: True
Assignment Operators (অ্যাসাইনমেন্ট অপারেটর)
ভ্যারিয়েবলে ভ্যালু অ্যাসাইন করার জন্য ব্যবহৃত হয়।
| অপারেটর | উদাহরণ | সমতুল্য |
|---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
//= |
x //= 3 |
x = x // 3 |
%= |
x %= 3 |
x = x % 3 |
**= |
x **= 3 |
x = x ** 3 |
count = 0
count += 1 # count is now 1 (equivalent to count = count + 1)
print(f"Count after += 1: {count}")
total = 100
total -= 20 # total is now 80
print(f"Total after -= 20: {total}")
price = 5
price *= 3 # price is now 15
print(f"Price after *= 3: {price}")
value = 10
value /= 2 # value is now 5.0 (division always results in float)
print(f"Value after /= 2: {value}")
num = 17
num //= 5 # num is now 3
print(f"Num after //= 5: {num}")
rem = 17
rem %= 5 # rem is now 2
print(f"Rem after %= 5: {rem}")
base = 2
base **= 4 # base is now 16
print(f"Base after **= 4: {base}")
Membership Operators (সদস্যতা অপারেটর)
কোনো সিকোয়েন্স (যেমন string, list, tuple) এ নির্দিষ্ট ভ্যালু আছে কিনা তা পরীক্ষা করে।
| অপারেটর | নাম | উদাহরণ | ফলাফল |
|---|---|---|---|
in |
Returns True if a value is found in the sequence |
5 in [1, 2, 3, 4, 5] |
True |
not in |
Returns True if a value is NOT found in the sequence |
'a' not in 'hello' |
True |
my_list = [10, 20, 30, 40, 50]
print(f"Is 30 in my_list? {30 in my_list}") # Output: Is 30 in my_list? True
print(f"Is 60 in my_list? {60 in my_list}") # Output: Is 60 in my_list? False
my_string = "Hello Python"
print(f"Is 'P' in my_string? {'P' in my_string}") # Output: Is 'P' in my_string? True
print(f"Is 'java' in my_string? {'java' in my_string}") # Output: Is 'java' in my_string? False (case sensitive)
print(f"Is 100 not in my_list? {100 not in my_list}") # Output: Is 100 not in my_list? True
print(f"Is 'H' not in my_string? {'H' not in my_string}") # Output: Is 'H' not in my_string? False
Identity Operators (পরিচয় অপারেটর)
দুটি ভ্যারিয়েবল একই অবজেক্ট (মেমরি লোকেশন) নির্দেশ করছে কিনা তা পরীক্ষা করে।
| অপারেটর | নাম | উদাহরণ (x is y) |
|---|---|---|
is |
Returns True if both variables are the same object |
x is y |
is not |
Returns True if both variables are NOT the same object |
x is not y |
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(f"a is b: {a is b}") # Output: a is b: False (different objects in memory, even if content is same)
print(f"a == b: {a == b}") # Output: a == b: True (content is the same)
print(f"a is c: {a is c}") # Output: a is c: True (c refers to the same object as a)
x = 10
y = 10
print(f"x is y (for small integers): {x is y}") # Output: True (Python often reuses immutable objects like small integers)
s1 = "hello"
s2 = "hello"
print(f"s1 is s2 (for strings): {s1 is s2}") # Output: True (String interning)
print(f"a is not b: {a is not b}") # Output: a is not b: True
print(f"a is not c: {a is not c}") # Output: a is not c: False
isঅপারেটর মেমরি লোকেশন চেক করে, যেখানে==অপারেটর ভ্যালু চেক করে।- সাধারণত
listএবং অন্যান্য mutable অবজেক্টের ক্ষেত্রেa is bএবংa == bভিন্ন ফলাফল দিতে পারে। - Immutable অবজেক্ট যেমন integer, string এর ক্ষেত্রে Python অপটিমাইজেশনের জন্য একই অবজেক্ট পুনঃব্যবহার করতে পারে।
2. Expressions (এক্সপ্রেশন)
এক্সপ্রেশন হল ভ্যালু, ভ্যারিয়েবল এবং অপারেটরের সমন্বয় যা একটি একক ভ্যালু তৈরি করে।
# Examples of expressions:
result1 = 5 + 3 # 8 (Arithmetic expression)
is_adult = age >= 18 # True or False (Comparison expression leading to boolean)
can_proceed = is_adult and has_permission # True or False (Logical expression)
name_greeting = "Hello, " + user_name # String concatenation (String expression)
x = 10
y = 20
z = (x + y) * 2 - (y / x) ** 2 # A more complex expression
# z = (30) * 2 - (20 / 10) ** 2
# z = 60 - (2) ** 2
# z = 60 - 4
# z = 56.0
print(f"Value of z: {z}")
- প্রতিটি এক্সপ্রেশন evaluate হওয়ার পর একটি নির্দিষ্ট ডেটা টাইপের ভ্যালু রিটার্ন করে।
3. Operator Precedence (অপারেটরের অগ্রাধিকার)
একাধিক অপারেটর একটি এক্সপ্রেশনে থাকলে কোন অপারেটরের কাজ আগে হবে তা Operator Precedence দ্বারা নির্ধারিত হয়।
| অগ্রাধিকার (উচ্চ থেকে নিম্ন) | অপারেটর |
|---|---|
| 1 | () (Parentheses) |
| 2 | ** (Exponentiation) |
| 3 | +x, -x (Unary plus/minus) |
| 4 | *, /, //, % (Multiplication, Division, Modulus) |
| 5 | +, - (Addition, Subtraction) |
| 6 | <<, >> (Bitwise shifts) |
| 7 | & (Bitwise AND) |
| 8 | ^ (Bitwise XOR) |
| 9 | | (Bitwise OR) |
| 10 | ==, !=, >, >=, <, <= (Comparison operators) |
| 11 | is, is not (Identity operators) |
| 12 | in, not in (Membership operators) |
| 13 | not (Logical NOT) |
| 14 | and (Logical AND) |
| 15 | or (Logical OR) |
# Example of precedence
result = 10 + 5 * 2 # Multiplication (*) has higher precedence than addition (+)
# result = 10 + (5 * 2)
# result = 10 + 10
# result = 20
print(f"10 + 5 * 2 = {result}") # Output: 20
result_with_parens = (10 + 5) * 2 # Parentheses change the order
# result_with_parens = (15) * 2
# result_with_parens = 30
print(f"(10 + 5) * 2 = {result_with_parens}") # Output: 30
# Another example
# 3 > 2 and 4 < 1 or not 5 == 5
# (3 > 2) and (4 < 1) or (not (5 == 5))
# True and False or (not True)
# True and False or False
# False or False
# False
print(3 > 2 and 4 < 1 or not 5 == 5) # Output: False
- সন্দেহ থাকলে,
()(parentheses) ব্যবহার করে এক্সপ্রেশনের কোন অংশ আগে evaluate হবে তা স্পষ্টভাবে নির্ধারণ করা ভালো।
Summary
- Operators বিভিন্ন ধরনের হয়: Arithmetic, Comparison, Logical, Assignment, Membership, Identity।
- Expressions ভ্যালু, ভ্যারিয়েবল এবং অপারেটরের সমন্বয়ে গঠিত হয় যা একটি একক ভ্যালু তৈরি করে।
- Operator Precedence নির্ধারণ করে কোন অপারেটরের কাজ আগে হবে। Parentheses
()ব্যবহার করে এই ক্রম পরিবর্তন করা যায়।
Homeworks
PY4H1: Basic Arithmetic Practice
- দুটি ভ্যারিয়েবল
num1এবংnum2নিন (যেমন,num1 = 15,num2 = 4)। - এই দুটি সংখ্যার যোগ, বিয়োগ, গুণ, ভাগ, ফ্লোর ডিভিশন, এবং মডুলাস নির্ণয় করে প্রিন্ট করুন।
num1কেnum2এর পাওয়ার (घात) হিসেবে নির্ণয় করে প্রিন্ট করুন।
PY4H2: Comparison Challenges
- ইউজারের কাছ থেকে তার বয়স (age) ইনপুট নিন।
- নিচের কন্ডিশনগুলো চেক করে
TrueবাFalseপ্রিন্ট করুন:- বয়স ১৮ এর সমান বা বেশি কিনা?
- বয়স ৬৫ এর কম কিনা?
- বয়স ২১ এর সমান নয় কিনা?
- দুটি স্ট্রিং ভ্যারিয়েবল নিন (যেমন,
str1 = "Python",str2 = "python")।- এরা সমান কিনা (
==)? - এরা অভিন্ন কিনা (
is)? (যদি Python স্ট্রিং ইন্টার্নিং করে, ফলাফল কী হবে?) str1কিstr2এর থেকে ছোট (<)?
- এরা সমান কিনা (
PY4H3: Logical Conditions
- তিনটি বুলিয়ান ভ্যারিয়েবল নিন:
is_sunny = True,is_warm = True,is_weekend = False। - নিচের বাক্যগুলোর জন্য লজিক্যাল এক্সপ্রেশন তৈরি করে ফলাফল প্রিন্ট করুন:
- “It’s a good day for a picnic if it’s sunny AND warm.”
- “We can go to the beach if it’s sunny AND warm, OR if it’s the weekend.”
- “It’s NOT a good day for skiing if it’s warm.”
PY4H4: Assignment Operator Tasks
- একটি ভ্যারিয়েবল
score = 0দিয়ে শুরু করুন। - Assignment operator ব্যবহার করে নিচের কাজগুলো করুন এবং প্রতিটি ধাপের পর
scoreপ্রিন্ট করুন:scoreএর সাথে ১০ যোগ করুন।scoreথেকে ৫ বিয়োগ করুন।scoreকে ২ দিয়ে গুণ করুন।scoreকে ৩ দিয়ে ভাগ করুন (ফ্লোটিং পয়েন্ট ডিভিশন)।scoreকে ২ দিয়ে ফ্লোর ডিভিশন করুন।
PY4H5: Expression Evaluation
- নিচের এক্সপ্রেশনগুলোর মান কী হবে তা প্রথমে নিজে খাতায় বের করুন, তারপর পাইথনে কোড লিখে যাচাই করুন:
10 * 3 - 2 ** 3 + (5 // 2)not (10 > 20 or 5 == 5 and True)"apple" in ["banana", "orange", "apple"] and "p" in "Python"
কোনো ভুল পেয়েছেন? LinkedIn-এ জানান।