ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
စာစစ်တဲ့ ဆရာတစ်ယောက်ကို စဉ်းစားကြည့်ပါ။ ဖြေလွှာ ထပ်ထဲက စာရွက်ကို တစ်ရွက်ချင်း ယူပြီး အမှတ်ပေးကာ ထပ်ကုန်တာနဲ့ ရပ်ပါတယ်။ ဒါက for loop ပါ — စုထားတဲ့ အရာတွေကို တစ်ခုချင်း လုပ်ပြီး ကုန်ရင် ရပ်ပါတယ်။
while loop ကတော့ ရေခဲသေတ္တာထဲ ရေခဲ ရှိသေးသရွေ့ ရေခဲ ထုတ်ယူနေတာမျိုးပါ။ ဘယ်နှခါ လုပ်ရမလဲ ကြိုမသိဘဲ "အခြေအနေ မှန်နေသေးလား" ကို အကြိမ်တိုင်း ပြန်စစ်ပါတယ်။ break က "ဒီမှာ ရပ်တော့"၊ continue က "ဒီတစ်ခု ကျော်ပြီး နောက်တစ်ခု ဆက်လုပ်" လို့ ဆိုလိုပါတယ်။
Problem → Why → Solution
ပြဿနာ: loop ပတ်နေရင်း list ကို ဖျက်တာ
Developer တစ်ယောက်က 50 အောက် အမှတ်တွေကို list ကနေ ဖယ်ချင်လို့ for loop ထဲမှာ remove() ကို ခေါ်ပါတယ်။ Error မတက်ပေမယ့် ရလဒ်မှာ 45 ကျန်နေပါတယ်။ နောက် developer တစ်ယောက်ကတော့ retry loop ရေးရင်း counter ကို လျှော့ဖို့ မေ့သွားလို့ program က ဘယ်တော့မှ မရပ်ဘဲ CPU ကို အပြည့် သုံးနေပါတော့တယ်။
scores = [40, 45, 90, 30]
for s in scores:
if s < 50:
scores.remove(s) # shifts the remaining items left
print(scores) # [45, 90] -- 45 was skipped, never checked
attempts = 3
while attempts > 0:
print("trying...")
# forgot: attempts -= 1 -> runs foreverဘာကြောင့် ဒီလိုဖြစ်တာလဲ
for loop က list ကို index 0, 1, 2 ... အစဉ်နဲ့ ဖတ်ပါတယ်။ 40 ကို ဖယ်လိုက်တော့ 45 က index 0 ကို ရွှေ့သွားပေမယ့် loop ကတော့ index 1 (90) ကို ဆက်ဖတ်လို့ 45 ကို လုံးဝ မစစ်လိုက်ရပါဘူး။ while loop မှာတော့ condition ကို False ဖြစ်စေမယ့် ပြောင်းလဲမှု body ထဲမှာ မရှိရင် ဘယ်တော့မှ မရပ်ပါဘူး။ ဖြေရှင်းနည်းက list အသစ် တည်ဆောက်တာနဲ့ while တိုင်းမှာ ရပ်မယ့် လမ်းကြောင်း သေချာ ထားတာပါ။
Core concept: iterable ကို စားသုံးတဲ့ for နဲ့ condition စစ်တဲ့ while
for item in iterable: က list, tuple, string, set, dict, range လို iterable တစ်ခုခုကနေ item တစ်ခုချင်း ယူပြီး variable ထဲ ထည့်ကာ body ကို run ပါတယ်။ Item ကုန်တာနဲ့ loop က အလိုအလျောက် ရပ်လို့ counter ကို ကိုယ်တိုင် စီမံစရာ မလိုပါဘူး။ String ပေါ်မှာ loop ပတ်ရင် character တစ်လုံးချင်း၊ dict ပေါ်မှာဆိုရင် key တစ်ခုချင်း ရပါတယ်။ Body ထဲမှာ loop variable ကို ပြန် assign လုပ်လည်း list က မပြောင်းပါဘူး၊ loop ပြီးသွားရင်လည်း variable ထဲမှာ နောက်ဆုံး item ကျန်နေပါတယ်။
အကြိမ်အရေအတွက် သတ်မှတ်ပြီး ထပ်လုပ်ချင်ရင် range() ကို သုံးပါတယ်။ range(5) က 0 ကနေ 4 အထိ၊ range(2, 11, 3) က 2, 5, 8 ကို ပေးပါတယ် — stop value ကို ဘယ်တော့မှ မပါဝင်ပါဘူး။ Index ရော value ပါ လိုရင် range(len(items)) အစား enumerate(items, start=1) ကို သုံးပြီး list နှစ်ခုကို တွဲပတ်ချင်ရင် zip(names, marks) ကို သုံးပါ၊ zip က တိုတဲ့ list ကုန်တာနဲ့ ရပ်ပါတယ်။
while condition: က body ကို မ run ခင်တိုင်း condition ကို စစ်ပြီး False ဖြစ်တာနဲ့ ရပ်ပါတယ်။ ဘယ်နှကြိမ် လိုမလဲ ကြိုမသိတဲ့ အခြေအနေ — user မှန်တာ ရိုက်ထည့်တဲ့အထိ မေးတာ၊ ငွေ ပစ်မှတ်ရောက်တဲ့အထိ တွက်တာ — အတွက် သင့်တော်ပါတယ်။ Body ထဲမှာ condition ကို နောက်ဆုံး False ဖြစ်စေမယ့် ပြောင်းလဲမှု အမြဲ ပါရပါမယ်။
break က အနီးဆုံး loop ကို ချက်ချင်း အပြီးထွက်ပြီး continue က လက်ရှိ round ရဲ့ ကျန်တဲ့ code ကို ကျော်ပြီး နောက် item ဆီ သွားပါတယ်။ Python မှာ loop တွေမှာ else clause လည်း ရှိပါတယ် — loop က break မလုပ်ဘဲ ပုံမှန် ပြီးဆုံးမှသာ else block run ပါတယ်။ ဒါကြောင့် "ရှာတယ်၊ တွေ့ရင် break၊ မတွေ့ရင် else" ပုံစံကို flag variable မလိုဘဲ ရေးလို့ရပါတယ်။
- for: နောက် item ကို ယူ
- Body ကို run (continue က ကျော်)
- Item ကုန်ရင် (ဒါမှမဟုတ် break) ရပ်
- while: condition ကို အရင်စစ်
Loop else က "break မဖြစ်ခဲ့ဘူး" လို့ ဆိုလိုပါတယ် — "loop ပြီးသွားတိုင်း" လို့ မဆိုလိုပါဘူး။
အထွေထွေ ပုံစံ
for item in iterable:
if skip_condition:
continue
if stop_condition:
break
process(item)
else:
no_break_happened()
while condition:
do_work()
update_something_the_condition_uses()for loop ထဲမှာ continue က item ကို ကျော်ပြီး break က loop တစ်ခုလုံးကနေ ထွက်ပါတယ်။ else က break မဖြစ်မှ run ပါတယ်။ while loop ရဲ့ နောက်ဆုံး line က condition ကို ပြောင်းစေမယ့် အဆင့်ပါ — အဲဒါ မရှိရင် infinite loop ဖြစ်ပါတယ်။
Example: အောင်တဲ့ အမှတ်တွေနဲ့ ကျန်တဲ့ ကြိုးစားခွင့်
ပထမ loop က အမှတ်စာရင်းကို enumerate() နဲ့ ပတ်ပြီး 50 အောက်ကို continue နဲ့ ကျော်ကာ ကျန်တာကို နံပါတ်နဲ့တကွ print ထုတ်ပါတယ်။ ဒုတိယ loop က attempts ကုန်တဲ့အထိ while နဲ့ ရေတွက်ပါတယ်။
scores = [68, 42, 91, 77]
for index, score in enumerate(scores, start=1):
if score < 50:
continue
print(f"{index}: {score}")
attempts = 3
while attempts > 0:
print(f"Attempts left: {attempts}")
attempts -= 1Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 3
for index, score in enumerate(scores, start=1):enumerate() က (နံပါတ်, value) အတွဲတွေ ပေးပြီး start=1 ကြောင့် နံပါတ်က 1 ကနေ စပါတယ်။ Loop က အဲဒီ အတွဲကို index နဲ့ score ထဲ ခွဲထည့်ပါတယ်။
- Lines 4-5
continue42 က 50 အောက်ဖြစ်လို့ continue က print ကို ကျော်ပြီး နောက် score ဆီ တန်းသွားပါတယ်။ ဒါကြောင့် output မှာ 2 မပါဘဲ 1, 3, 4 ပဲ ပါပါတယ်။
- Line 9
while attempts > 0:Body ကို မ run ခင်တိုင်း attempts ကို စစ်ပါတယ်။ 3, 2, 1 မှာ True ဖြစ်ပြီး 0 ရောက်တော့ False ဖြစ်လို့ loop ရပ်ပါတယ်။
- Line 11
attempts -= 1ဒီ line က condition ကို နောက်ဆုံး False ဖြစ်စေတဲ့ ပြောင်းလဲမှုပါ။ ဖယ်လိုက်ရင် "Attempts left: 3" ကို အဆုံးမရှိ print ထုတ်နေပါမယ်။
1: 68
3: 91
4: 77
Attempts left: 3
Attempts left: 2
Attempts left: 1range, zip နဲ့ loop else
အောက်က code က range() ရဲ့ step argument၊ list နှစ်ခုကို zip() နဲ့ တွဲပတ်တာနဲ့ ရှာဖွေတဲ့ loop မှာ else ကို သုံးတာကို ပြပါတယ်။ target = 50 ကို ဘယ်သူမှ မရလို့ break မဖြစ်ဘဲ else block က "nobody scored 50" ကို print ထုတ်ပါတယ်။ target ကို 47 ပြောင်းကြည့်ရင် found Bo ထွက်ပြီး else မ run တော့ပါဘူး။
for n in range(2, 11, 3):
print("range gives", n)
names = ["Aye", "Bo", "Chit"]
marks = [81, 47, 66]
for name, mark in zip(names, marks):
print(f"{name}: {mark}")
target = 50
for name, mark in zip(names, marks):
if mark == target:
print("found", name)
break
else:
print("nobody scored", target)လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
CSV file တစ်ခုက row တွေကို import လုပ်တဲ့ script မှာ for loop က row တစ်ခုချင်းကို ဖတ်ပြီး မှန်တဲ့ row တွေကို valid list ထဲ၊ မှားတဲ့ row တွေကို enumerate() ကပေးတဲ့ row နံပါတ်နဲ့တကွ errors list ထဲ ခွဲထည့်ပါတယ်။ ဒါမှ user ကို "row 17 မှာ email မှားနေတယ်" လို တိကျတဲ့ message ပြနိုင်ပါတယ်။ Empty file ဖြစ်ရင် loop တစ်ခါမှ မ run တာကိုလည်း test လုပ်ထားရပါမယ်။
Network request ကို ပြန်ကြိုးစားတဲ့ code တွေက while loop ကို သုံးပါတယ်။ ဒါပေမယ့် production မှာ attempts အများဆုံး အရေအတွက်၊ ကြိုးစားတိုင်းကြား စောင့်ချိန် တိုးလာတာ (backoff) နဲ့ timeout ကို အမြဲ ထည့်ထားပါတယ်၊ server ပြုတ်နေရင် infinite loop က server ကို ပိုဆိုးအောင် လုပ်လို့ပါ။ Game loop တွေနဲ့ queue ထဲက message တွေကို ဆွဲဖတ်တဲ့ worker တွေကလည်း while loop ပေါ် အခြေခံပြီး shutdown signal ရောက်ရင် break နဲ့ ထွက်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Iterate လုပ်နေတဲ့ list ကို ပြင်တာ
Problem section မှာ မြင်ခဲ့သလို loop ထဲမှာ remove() လုပ်ရင် item တွေ ကျော်သွားပါတယ်။ passing = [] ကို အရင် ဖန်တီးပြီး loop ထဲမှာ ထားချင်တဲ့ item ကိုပဲ append() လုပ်ပါ ဒါမှမဟုတ် for s in scores[:] လို copy ပေါ်မှာ ပတ်ပါ။
2. while loop မှာ condition ကို မပြောင်းတာ
Counter လျှော့ဖို့ မေ့တာ၊ input ကို loop အပြင်မှာပဲ တစ်ခါ ဖတ်တာလို အမှားတွေက infinite loop ဖြစ်စေပါတယ်။ Loop ရေးပြီးတိုင်း "ဘယ် line က condition ကို False ဖြစ်စေမလဲ" ဆိုတာကို ထောက်ပြနိုင်ရပါမယ်။ Terminal မှာ ရပ်ချင်ရင် Ctrl+C နှိပ်ပါ။
3. range() ရဲ့ stop ပါမယ်ထင်တာ
range(1, 10) က 1 ကနေ 9 အထိပဲ ပေးပါတယ်။ 1 ကနေ 10 အထိ လိုရင် range(1, 11) လို့ ရေးရပါတယ်။ Off-by-one error က loop bug တွေထဲမှာ အဖြစ်အများဆုံးပါ။
4. Loop else ကို "if not found" အစား "အမြဲ" လို့ ထင်တာ
Loop else က loop ပြီးတိုင်း run တာ မဟုတ်ဘဲ break မဖြစ်မှသာ run ပါတယ်။ else ကို loop body ထဲက if နဲ့ မှားပြီး indent လုပ်မိရင် item တိုင်းအတွက် "not found" ထွက်ပါတယ်၊ ဒါကြောင့် else ကို for keyword နဲ့ တန်းတူ indent ထားပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Collection ပေါ်က item တွေကို တိုက်ရိုက် ပတ်ပါ — for i in range(len(items)) ကို index အစစ်အမှန် လိုမှသာ သုံးပါ။
- Result ကို list အသစ်ထဲ စုတာက ရှိပြီးသား list ကို loop ထဲမှာ ပြင်တာထက် ဘေးကင်းပါတယ်။
- while loop တိုင်းမှာ အများဆုံး အကြိမ်ရေ ကန့်သတ်ချက် ထည့်ထားပါ၊ အထူးသဖြင့် network နဲ့ input အတွက်ပါ။
Mini challenge
Mini challenge: order စာရင်း စစ်ခြင်း
User ရိုက်ထည့်ထားတဲ့ quantity string တွေကို loop ပတ်ပြီး မှန်တာတွေ ပေါင်း၊ မှားတာတွေရဲ့ position ကို မှတ်၊ "STOP" တွေ့ရင် ရပ်ပါ။ ပြီးရင် while loop နဲ့ စုငွေ 150 ရောက်ဖို့ ဘယ်နှလ ကြာမလဲ တွက်ပါ။
- enumerate(entries, start=1) နဲ့ position ကို ယူရမယ်
- "STOP" တွေ့ရင် position ကို print ပြီး break လုပ်ရမယ် (STOP နောက်က "9" ကို မပေါင်းရ)
- isdigit() မဟုတ်တဲ့ entry ကို bad list ထဲ position ထည့်ပြီး continue လုပ်ရမယ်
- STOP မတွေ့ရင် loop else နဲ့ "no stop marker found" ပြရမယ်
- 100 ကနေ လစဉ် 10% တိုးတဲ့ balance 150 ရောက်တဲ့အထိ while နဲ့ ရေတွက်ရမယ်
entries = ["3", "x", "5", "-2", "4", "STOP", "9"]
total = 0
bad = []
# TODO: for position, text in enumerate(...):
# STOP -> print the position and break
# not text.isdigit() -> remember the position, continue
# otherwise add int(text) to total
# TODO: loop else -> print "no stop marker found"
# TODO: print total and bad
balance = 100
months = 0
# TODO: while balance is below 150, grow it by 10% and count the month
# TODO: print months and the rounded balanceရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: stop marker at 6 total: 12 bad entries at: [2, 4] 5 months -> 161.05
အရိပ်အမြွက် ကြည့်ရန်
"-2".isdigit() က False ဖြစ်လို့ အနုတ် number ကိုလည်း မှားတဲ့ entry အဖြစ် သတ်မှတ်ပါတယ်။ else ကို for နဲ့ တန်းတူ indent ထားပါ၊ if နဲ့ မဟုတ်ပါ။ Balance ကို print ထုတ်တဲ့အခါ round(balance, 2) သုံးပါ။
အဖြေ ကြည့်ရန်
entries = ["3", "x", "5", "-2", "4", "STOP", "9"]
total = 0
bad = []
for position, text in enumerate(entries, start=1):
if text == "STOP":
print("stop marker at", position)
break
if not text.isdigit():
bad.append(position)
continue
total += int(text)
else:
print("no stop marker found")
print("total:", total)
print("bad entries at:", bad)
balance = 100
months = 0
while balance < 150:
balance = balance * 1.1
months += 1
print(months, "months ->", round(balance, 2))Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- for က iterable ထဲက item တစ်ခုချင်းကို ယူပြီး ကုန်ရင် ရပ်ပါတယ်၊ while က condition False ဖြစ်မှ ရပ်ပါတယ်။
- range() က stop ကို မပါဝင်ပြီး enumerate() က index ကို၊ zip() က list နှစ်ခုကို တွဲပေးပါတယ်။
- break က loop ကနေ ထွက်ပြီး continue က လက်ရှိ round ကို ကျော်ပါတယ်၊ else က break မဖြစ်မှ run ပါတယ်။
- Loop ပတ်နေတဲ့ list ကို မပြင်ပါနဲ့၊ while တိုင်းမှာ ရပ်မယ့် လမ်းကြောင်း သေချာ ထားပါ။
The Python Tutorial — Python Software Foundation