ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Operator ဆိုတာ value တွေပေါ်မှာ အလုပ်တစ်ခု လုပ်ခိုင်းတဲ့ သင်္ကေတ ဒါမှမဟုတ် keyword လေးတွေပါ။ + က ပေါင်းခိုင်း၊ > က နှိုင်းယှဉ်ခိုင်း၊ and က condition တွေ ချိတ်ခိုင်းပါတယ်။ Calculator ရဲ့ ခလုတ်တွေလိုပဲ — ခလုတ်တိုင်းက အလုပ်တစ်မျိုးစီ လုပ်ပါတယ်။
Python မှာ operator တွေကို အလုပ်ပေါ်မူတည်ပြီး အုပ်စု ခြောက်ခု ခွဲထားပါတယ်။ ဂဏန်းတွက်တာ၊ နှိုင်းယှဉ်တာ၊ logic ပေါင်းတာ၊ variable ကို update လုပ်တာ၊ collection ထဲ ပါမပါ စစ်တာ၊ object တစ်ခုတည်း ဟုတ်မဟုတ် စစ်တာ ဖြစ်ပါတယ်။ Expression တစ်ခုထဲမှာ operator အများကြီး ပါလာရင် ဘယ်ဟာ အရင် အလုပ်လုပ်လဲ ဆိုတာလည်း သိထားရပါမယ်။
Problem → Why → Solution
ပြဿနာ: ပျမ်းမျှ တွက်တာ မှားနေတယ်
စာမေးပွဲ အမှတ် သုံးခုရဲ့ ပျမ်းမျှကို တွက်ချင်လို့ 80၊ 90၊ 70 ကို ပေါင်းပြီး 3 နဲ့ စားတဲ့ code ရေးလိုက်ပါတယ်။ အဖြေက 80 ဖြစ်ရမှာပါ၊ ဒါပေမယ့် program က 193.33 လို့ ပြပါတယ် — အမှတ်အများဆုံးထက်တောင် ကြီးနေပါတယ်။ Error မတက်တော့ program က ဘာမှားလဲ မပြောပါဘူး။
math_score = 80
english_score = 90
science_score = 70
average = math_score + english_score + science_score / 3
print(round(average, 2)) # 193.33, not 80.0ဘာကြောင့် မှားတာလဲ
Operator တွေမှာ precedence (ဦးစားပေး အစဉ်) ရှိပါတယ်။ / က + ထက် ဦးစားပေးမှု မြင့်လို့ Python က science_score / 3 (23.33) ကို အရင်တွက်ပြီးမှ 80 နဲ့ 90 ကို ပေါင်းပါတယ်။ ကျောင်းက သင်္ချာ စည်းမျဉ်းအတိုင်းပါပဲ။ (math_score + english_score + science_score) / 3 လို့ ကွင်းခတ်လိုက်မှ ပေါင်းတာကို အရင် လုပ်ပါမယ်။
Core concept: operator အုပ်စု ခြောက်ခုနဲ့ precedence
| အုပ်စု | Operator များ | ရလဒ် |
|---|---|---|
| Arithmetic | + - * / // % ** | Number |
| Comparison | == != > < >= <= | True / False |
| Logical | and or not | Operand တစ်ခု ဒါမှမဟုတ် bool |
| Assignment | = += -= *= /= //= %= **= | Variable ကို update |
| Membership | in not in | True / False |
| Identity | is is not | True / False |
Arithmetic operator တွေက number အသစ် ထုတ်ပေးပြီး comparison၊ membership နဲ့ identity operator တွေက True/False ထုတ်ပေးပါတယ်။ Operator အများစုက type ပေါ်မူတည်ပြီး အဓိပ္ပာယ် ပြောင်းပါတယ် — + က number တွေကို ပေါင်းပေမယ့် string နဲ့ list တွေကို ဆက်ပေးပါတယ်။ in က list ထဲမှာ element ကို ရှာပြီး string ထဲမှာ substring ကို ရှာကာ dictionary ထဲမှာတော့ key ကို ရှာပါတယ်။
Augmented assignment ဖြစ်တဲ့ c += 3 က c = c + 3 ရဲ့ အတိုကောက်ပါ။ Loop ထဲမှာ total += price လို စုစုပေါင်း တွက်တဲ့အခါ အသုံးအများဆုံးပါ။ Python မှာ C/JavaScript လို ++ operator မရှိလို့ count += 1 လို့ ရေးရပါတယ်။
== နဲ့ is က မတူပါဘူး။ == က value နှစ်ခု တန်ဖိုး တူမတူ မေးပြီး is က name နှစ်ခုဟာ memory ထဲက object တစ်ခုတည်းကို ညွှန်နေလား မေးပါတယ်။ [1, 2] == [1, 2] က True ဖြစ်ပေမယ့် list နှစ်ခု သီးခြားစီ ဖန်တီးထားလို့ is ကတော့ False ပါ။ ဒါကြောင့် is ကို x is None လို singleton စစ်တဲ့ နေရာမှာပဲ သုံးပါ။
Precedence အစဉ်ကို အမြင့်ဆုံးကနေ ကြည့်ရင် ** ၊ ပြီးရင် * / // % ၊ ပြီးရင် + - ၊ ပြီးရင် comparison နဲ့ in/is ၊ ပြီးရင် not ၊ and ၊ နောက်ဆုံး or ပါ။ ** က ညာဘက်ကနေ စတွက်လို့ 2 ** 3 ** 2 က 512 ဖြစ်ပြီး -2 ** 2 က -4 ပါ (** က minus sign ထက် အရင်)။ ကျန်တဲ့ အဆင့်တူ operator တွေကတော့ ဘယ်ကနေ ညာ တွက်ပါတယ်။
- ကွင်းထဲက အရင်
- ** ပြီးမှ * / // %
- + - ပြီးမှ comparison
- not, and, or နောက်ဆုံး
သံသယ ရှိရင် ကွင်းခတ်ပါ — ကွင်းက ရလဒ်ကို မှန်စေပြီး code ဖတ်တဲ့သူကိုလည်း ကူညီပါတယ်။
အထွေထွေ ပုံစံ
result = (left + right) * factor # arithmetic, grouped
is_valid = low <= value <= high # comparison
ok = condition_a and not condition_b # logical
counter += step # same as counter = counter + step
found = item in collection # membership
missing = value is None # identityအုပ်စု တစ်ခုချင်းစီရဲ့ ပုံစံ တစ်ကြောင်းစီပါ။ ကွင်းက precedence ကို ကျော်ပြီး += က ဘယ်ဘက် variable ကို update လုပ်ပါတယ်။ in က ပါမပါ၊ is က object တစ်ခုတည်း ဟုတ်မဟုတ် စစ်ပါတယ်။
Example: operator အုပ်စု ခြောက်ခုလုံး
ဒီ example က အုပ်စုတစ်ခုချင်းကို အပိုင်းတစ်ပိုင်းစီနဲ့ စမ်းပါတယ်။ နောက်ဆုံး အပိုင်းက identity operator (is) အတွက်ပါ — == က True ဖြစ်ပြီး is က False ဖြစ်တဲ့ အခြေအနေကို ကိုယ်တိုင် မြင်ရပါမယ်။ Arithmetic နဲ့ logical အပိုင်းတွေက Numbers နဲ့ Booleans lesson တွေနဲ့ ရည်ရွယ်ချက်ရှိရှိ ထပ်နေတာပါ၊ ဒါမှ ဒီ file တစ်ခုတည်းက အုပ်စုတိုင်းအတွက် cheat sheet အဖြစ် အသုံးဝင်မှာပါ။
# ===== 1. Arithmetic Operators =====
x = 10
y = 3
print("===== Arithmetic =====")
print(f"x + y = {x + y}") # 13
print(f"x - y = {x - y}") # 7
print(f"x * y = {x * y}") # 30
print(f"x / y = {x / y}") # 3.3333333333333335
print(f"x // y = {x // y}") # 3 (Floor Division)
print(f"x % y = {x % y}") # 1 (Modulus)
print(f"x ** y = {x ** y}") # 1000 (Exponentiation)
# ===== 2. Comparison Operators =====
print("\n===== Comparison =====")
a = 5
b = 10
print(f"a == b: {a == b}") # False
print(f"a != b: {a != b}") # True
print(f"a > b: {a > b}") # False
print(f"a < b: {a < b}") # True
# ===== 3. Logical Operators =====
print("\n===== Logical =====")
print(f"True and False: {True and False}")
print(f"True or False: {True or False}")
print(f"not True: {not True}")
# ===== 4. Assignment Operators =====
print("\n===== Assignment =====")
c = 5
c += 3 # c = c + 3
print(f"c += 3: {c}") # 8
c *= 2 # c = c * 2
print(f"c *= 2: {c}") # 16
# ===== 5. Membership Operators =====
print("\n===== Membership =====")
fruits = ["apple", "banana"]
print(f"'apple' in fruits: {'apple' in fruits}")
print(f"'mango' not in fruits: {'mango' not in fruits}")
# ===== 6. Identity Operators =====
print("\n===== Identity =====")
list_a = [1, 2]
list_b = [1, 2]
list_c = list_a
print(f"list_a == list_b: {list_a == list_b}") # same contents
print(f"list_a is list_b: {list_a is list_b}") # different objects
print(f"list_a is list_c: {list_a is list_c}") # same objectCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 9
print(f"x / y = {x / y}") # 3.3333333333333335/ က float ပြန်ပေးပြီး float ရဲ့ approximation ကြောင့် နောက်ဆုံး ဂဏန်းက 5 ဖြစ်နေပါတယ်။ // ကတော့ 3 ဆိုတဲ့ int ကို ပြန်ပေးပါတယ်။
- Lines 33-34
c += 3 # c = c + 3c က 5 ကနေ 8 ဖြစ်သွားပါတယ်။ နောက်တစ်ကြောင်း c *= 2 က 8 ကို 16 ဖြစ်စေပါတယ် — augmented assignment တစ်ခုချင်းက c ရဲ့ လက်ရှိ value ပေါ်မှာ ဆက်တွက်တာပါ။
- Line 42
print(f"'mango' not in fruits: {'mango' not in fruits}")not in က in ရဲ့ ဆန့်ကျင်ဘက်ပါ။ not ('mango' in fruits) လို့ ရေးတာနဲ့ ရလဒ်တူပေမယ့် ပိုဖတ်ရလွယ်ပါတယ်။
- Line 49
print(f"list_a == list_b: {list_a == list_b}") # same contentsList နှစ်ခုမှာ element တွေ တူလို့ == က True ပါ။
- Lines 50-51
print(f"list_a is list_c: {list_a is list_c}") # same objectlist_b က သီးခြား ဖန်တီးထားလို့ list_a is list_b က False ဖြစ်ပြီး list_c = list_a က object အသစ် မဖန်တီးဘဲ နာမည်နောက်တစ်ခုပဲ ပေးတာမို့ is က True ပါ။
===== Arithmetic =====
x + y = 13
x - y = 7
x * y = 30
x / y = 3.3333333333333335
x // y = 3
x % y = 1
x ** y = 1000
===== Comparison =====
a == b: False
a != b: True
a > b: False
a < b: True
===== Logical =====
True and False: False
True or False: True
not True: False
===== Assignment =====
c += 3: 8
c *= 2: 16
===== Membership =====
'apple' in fruits: True
'mango' not in fruits: True
===== Identity =====
list_a == list_b: True
list_a is list_b: False
list_a is list_c: TruePrecedence ကို ကိုယ်တိုင် စမ်းကြည့်ခြင်း
Precedence ကို ကျက်မှတ်တာထက် Python shell မှာ စမ်းကြည့်တာက ပိုမှတ်မိလွယ်ပါတယ်။ အောက်က expression တွေကို run မလုပ်ခင် ရလဒ်ကို ကြိုခန့်မှန်းကြည့်ပါ။ အထူးသဖြင့် -2 ** 2 နဲ့ (-2) ** 2 ကွာခြားချက်နဲ့ 10 - 4 - 3 ကို ဘယ်ကနေ ညာ တွက်တာကို သတိထားပါ — ညာကနေ တွက်ရင် 9 ရမှာ ဖြစ်ပေမယ့် Python က 3 ကို ပြန်ပေးပါတယ်။
print(2 + 3 * 4) # 14 (* before +)
print((2 + 3) * 4) # 20 (parentheses first)
print(2 ** 3 ** 2) # 512 (** groups from the right)
print(-2 ** 2, (-2) ** 2) # -4 4
print(10 - 4 - 3) # 3 (left to right)
print(1 + 2 == 3 and not 5 < 4) # Trueလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Business logic အများစုက operator အုပ်စု အမျိုးမျိုးကို ပေါင်းစပ်ထားတာပါ။ Shopping cart မှာ total += price * quantity နဲ့ စုစုပေါင်း တွက်ပြီး if total >= 50 and country in FREE_SHIPPING_COUNTRIES: နဲ့ ပို့ခ အခမဲ့ ဟုတ်မဟုတ် ဆုံးဖြတ်ပါတယ်။ Pagination မှာ page_count = (total_items + per_page - 1) // per_page နဲ့ စာမျက်နှာ အရေအတွက်ကို ရှာပါတယ်။
Membership test တွေက blocklist၊ allowed file extension၊ supported language စစ်တာတွေမှာ နေရာတိုင်း ရှိပါတယ်။ Data ကြီးလာရင် list ထဲမှာ in နဲ့ ရှာတာက element တစ်ခုချင်း စစ်ရလို့ နှေးပါတယ်၊ set ဒါမှမဟုတ် dict ထဲမှာ ရှာတာကတော့ အရွယ်အစား ဘယ်လောက်ပဲ ကြီးကြီး အမြဲ မြန်ပါတယ်။ is None ကတော့ function တစ်ခုက ရလဒ် ပြန်ပေးခဲ့လား စစ်တဲ့ code တိုင်းမှာ တွေ့ရပါတယ်။ အချိန်ကြာကြာ run ရတဲ့ script တွေမှာ if i % 1000 == 0: print(i) လို % ကို progress ပြဖို့ သုံးပါတယ်၊ ဒါမှ log မှာ record တစ်ခုချင်းအတွက် မဟုတ်ဘဲ record တစ်ထောင်မှာ တစ်ကြောင်းပဲ ပေါ်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. ကွင်း မခတ်ဘဲ ပျမ်းမျှ တွက်တာ
a + b + c / 3 က c ကိုပဲ 3 နဲ့ စားပါတယ်။ (a + b + c) / 3 လို့ ရေးပါ၊ ဒါမှမဟုတ် sum(scores) / len(scores) ကို သုံးပါ။ ရလဒ် တစ်ခု ဆန်းနေရင် precedence ကို အရင် သံသယ ဝင်ပါ။
2. Value နှိုင်းယှဉ်ဖို့ is ကို သုံးတာ
if name is "admin": က တစ်ခါတလေ အလုပ်လုပ်သလို ထင်ရပေမယ့် Python က string တွေကို memory မှာ မျှသုံးမသုံး ဆိုတာ implementation အပေါ် မူတည်ပါတယ်၊ Python 3.8 ကစပြီး SyntaxWarning လည်း ပြပါတယ်။ Value အတွက် == ကို၊ None၊ True၊ False အတွက်ပဲ is ကို သုံးပါ။
3. ++ ဒါမှမဟုတ် =+ လို့ ရေးတာ
count++ က SyntaxError ပါ။ ပိုအန္တရာယ်များတာက count =+ 1 ပါ — error မတက်ဘဲ count = +1 လို့ အဓိပ္ပာယ်ရလို့ count က 1 ပဲ အမြဲ ဖြစ်နေပါတယ်။ count += 1 ကို သုံးပါ။
4. && နဲ့ || ကို သုံးတာ
တခြား language တွေကနေ လာတဲ့သူတွေ if a > 0 && b > 0: လို့ ရေးမိလေ့ရှိပြီး SyntaxError ရပါတယ်။ Python ရဲ့ logical operator တွေက and၊ or၊ not ဆိုတဲ့ English keyword တွေဖြစ်ပြီး ! ကိုလည်း != ထဲမှာပဲ သုံးပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Operator တွေ ရောနေတဲ့ expression မှာ ကွင်းထည့်ပြီး ရည်ရွယ်ချက်ကို ရှင်းအောင် ပြပါ။
- x is None / x is not None ကို သုံးပြီး ကျန်တဲ့ နှိုင်းယှဉ်ချက် အားလုံးမှာ == ကို သုံးပါ။
- ခဏခဏ in နဲ့ စစ်ရမယ့် collection ကြီးတွေကို set အဖြစ် သိမ်းပါ။
Mini challenge
Mini challenge: leap year နဲ့ checkout
Operator အုပ်စု အမျိုးမျိုးကို ပေါင်းသုံးပြီး leap year စစ်တဲ့ function နဲ့ coupon လက်ခံတဲ့ checkout function ရေးပါ။
- is_leap(year) — 4 နဲ့ စားပြတ်ရမယ်၊ 100 နဲ့ စားပြတ်ရင် မဟုတ်ဘူး၊ 400 နဲ့ စားပြတ်ရင်တော့ ဟုတ်တယ် (% နဲ့ and/or သုံး)
- checkout(prices, coupon) — += နဲ့ ပေါင်းပြီး coupon က None မဟုတ်ဘဲ ("SAVE10", "WELCOME") ထဲ ပါရင် *= 0.9 လုပ်ရမယ်
- 2024, 1900, 2000, 2023 ကို စစ်ပြီး print ထုတ်ရမယ်
- မှန်တဲ့ ပျမ်းမျှနဲ့ ကွင်းမခတ်တဲ့ bug ပျမ်းမျှ နှစ်ခုလုံး print ထုတ်ရမယ်
def is_leap(year: int) -> bool:
# TODO: one return line using %, ==, !=, and, or
...
def checkout(prices: list[float], coupon: str | None) -> float:
total = 0.0
# TODO: add every price with +=
# TODO: if coupon is not None and is a known code, apply 10% off with *=
return round(total, 2)
# TODO: print is_leap for 2024, 1900, 2000, 2023
cart = [12.5, 7.5, 30.0]
# TODO: print checkout(cart, None) and checkout(cart, "SAVE10")
# TODO: print the correct average and the buggy one (no parentheses)ရလဒ်: Program က အောက်ပါအတိုင်း ထုတ်ပေးရပါမယ်: 2024: leap=True 1900: leap=False 2000: leap=True 2023: leap=False No coupon: 50.0 SAVE10: 45.0 Average price: 16.67 Buggy average: 30.0
အရိပ်အမြွက် ကြည့်ရန်
Leap year condition က year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) ပါ။ or အပိုင်းကို ကွင်းမခတ်ရင် and က အရင် အလုပ်လုပ်ပြီး ရလဒ် မှားသွားပါမယ်။
အဖြေ ကြည့်ရန်
def is_leap(year: int) -> bool:
"""Divisible by 4, except centuries, unless divisible by 400."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def checkout(prices: list[float], coupon: str | None) -> float:
total = 0.0
for price in prices:
total += price
if coupon is not None and coupon in ("SAVE10", "WELCOME"):
total *= 0.9
return round(total, 2)
for year in (2024, 1900, 2000, 2023):
print(f"{year}: leap={is_leap(year)}")
cart = [12.5, 7.5, 30.0]
print(f"No coupon: {checkout(cart, None)}")
print(f"SAVE10: {checkout(cart, 'SAVE10')}")
print(f"Average price: {sum(cart) / len(cart):.2f}")
print(f"Buggy average: {cart[0] + cart[1] + cart[2] / 3}")Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Operator အုပ်စု ခြောက်ခုက arithmetic, comparison, logical, assignment, membership, identity ပါ။
- Precedence အစဉ်က ** ၊ * / // % ၊ + - ၊ comparison ၊ not ၊ and ၊ or ဖြစ်ပြီး ကွင်းက အားလုံးကို ကျော်ပါတယ်။
- += က x = x + ... ရဲ့ အတိုကောက်ဖြစ်ပြီး Python မှာ ++ မရှိပါဘူး။
- == က value ကို၊ is က object identity ကို စစ်ပါတယ် — is ကို None အတွက်ပဲ သုံးပါ။
The Python Tutorial — Python Software Foundation