Skip to main content

Command Palette

Search for a command to run...

Python Conditionals with CS50

Week-1

Published
2 min read
Python Conditionals with CS50

Programmer တ‌စ်ယောက်အနေနဲ့ ကိုယ့်ရဲ့ program တွေကို ဘယ်လိုထိန်းချုပ်မလဲ?Conditionals? Yes!

Python programmer တယောက်နေနဲ့ Python မှာ conditionals တွေကို အသုံးပြုပြီး “ဘယ်အချိန်မှာတော့ဘာလုပ်ပါ၊ အဲ့တာမဟုတ်ရင်တော့ ကျန်တာပဲဆက်လုပ်လိုက်တော့” ဆိုတဲ့အခြေနေတွေကို ကိုယ်ကြိုက်သလို syntax(coding format) နဲ့ အညီ computer ကိုလုပ်ခိုင်းလို့ရပါတယ်။

ခိုင်းတာတော့ဟုတ်ပါပြီ၊ ဘယ်လိုတွေမေးပြီးညွှန်ကြားရမလဲ ကွန်ပြူတာမှာလဲ သင်္ချာနည်းအရမေးခွန်းတွေမေးဖို့ comparison operators တွေရှိပါတယ် အချို့ symbol တွေကတော့ အဓိပ္ပါယ်အတူတူပါပဲ။ greater than or equal to sign ကို သင်္ချာမှာ "⩾ "လို့ရေးပြီး၊ Python မှာကတော့ “>=” ဆိုပြီးရေးလို့ရပါတယ်။

တန်ဖိုးတစ်ခုနဲ့ယှဉ်ကြည့်ချင်တဲ့အခါတော့ အထက်မှာဖော်ပြခဲ့တဲ့ သင်္ကေတတွေကိုသုံးလို့ရပါတယ်။ ထိုသင်္ကေတကတော့ တစ်ခုတည်းသုံးလို့မရပါဘူး။ သူတို့နဲ့ အတူတူတွဲသုံးရတဲ့ statements တွေရှိပါတယ်။ (if, elif, else)

if ဆိုတာကတော့ သူ့ English meaning လိုပါပဲ

```

if (ဒါသာမှန်ရင်):

(ဒါလုပ်မယ်)

```

ဆိုပြီးသုံးရတာပါ။

x = 6
y = 12
if x < y:
    print("x is less than y")

Output : x is less than y

if, elif, else statement တွေရေးတဲ့ အခါ indentation(4 space) ကလိုအပ်ပါတယ်။

ဒါမှ computer က အပေါ်က if statement မှန်ရင် 4 space(1 tab) လုပ်ထားတဲ့ code အကြောင်းကို run သွားမှာပဲဖြစ်ပါတယ်။ statement တွေနောက်မှာ “:” ကျန်ခဲ့လို့လည်းမရပါဘူး။elif ကလဲထိုနည်းတူပါပဲ ဒါပေမယ့်သူက if statement or elif statement ရေးပြီး အောက်မှာမှရေးရတာပါ။

‌if condition စစ်လိုမှန်သွားရင် elif ကိုထပ်မစစ်တော့ပါဘူး။

if statement တွေကြီးပဲဆိုရင်တော့ သူကတစ်ခုပြီးတစ်ခုထပ်စစ်အုံးမှာပါ။

else ကတော့ အပေါ်ကတွေစစ်လို့ မှားရင် နောက်ဆုံးလုပ်ရမဲ့ဟာပါ။

x = 12
y = 6

if x < y:
    print("x is less than y")
elif x > y:
    print("x is greater than y")
else:
    print("x is equal to y")

Output : x is greater than y

if statement မှာတစ်ခုထက်မကတဲ့ condition ‌တွေကို စစ်ချင်တယ်ဆိုရင်တော့ Logical Operators တွေဖြစ်တဲ့ and နဲ့ or ကိုသုံးလို့ရပါတယ်။

and က condition နှစ်ခုစလုံးမှန်မှ True ထွက်ပါတယ်။

or ကတော့ condition တစ်ခုမှန်ရင်ကို True ထွက်ပါတယ်။

Statement တေကို သုံးပြီးတန်ဖိုးတွေကို နှိုင်းယှဉ်တဲ့အခါ ထွက်လာမယ့်အဖြေက True or False ဆိုတဲ့ Boolean value နှစ်ခုပဲရှိပါတယ်။ True ထွက်ခဲ့ရင် ဘာဆက်လုပ်ရမယ်၊ False ထွက်ခဲ့ရင်ဘာဆက်လုပ်ရမယ်ဆိုတာကိုတော့ programmer က ကွန်ပျူတာ‌ကို ကိုယ်လိုသလို အခြေအနေပေါ်မူတည်ပြီး ခိုင်းလို့ရတာပဲဖြစ်ပါတယ်။

x = 4
y = 0
if x != y:
    if x > y:
        print("x is greater than y")

VS

# Recommended way
x = 4
y = 0
if x != y and x > y:
    print("x is greater than y")

If, elif, else တို့လို statement တွေကို တော့ numeric တွေစစ်တဲ့ အခါမှာအများဆုံး အသုံးပြုပါတယ်။ eg (integer)

match, caseကိုတော့ pattern တွေ စစ်တဲ့အခါမှာအသုံးများပါတယ်။ eg (string

Match ကတော့ Case နဲ့ သုံးရပါတယ်

Match (ဒါထဲက):

Case (ဒီဟာဖြစ်ရင်):

(ဒါလုပ်မယ်)

match name:
    case "harry" | "hermione" | "ron":
        print("Gryffindor")
    case "draco":
        print("Slytherin")
    case _:
        print("Who? ")

အဲ့လိုနှိုင်းယှဉ်တဲ့အဆင့်တွေကိုရေးသားတဲ့အခါ python ရဲ့ သတ်မှတ်ထားတဲ့ Syntax တွေကို အတိအကျလိုက်နာဖို့လိုပါတယ်။ semicolon ":" လေးတစ်ခုကျန်တာ၊ spaceတစ်ခု လိုတာပိုတာနဲ့ကို syntax error ပေါ်ပေါက်ပြီး ရေးထားတဲ့ပရိုဂရမ်တစ်ခုလုံး အလုပ်မလုပ်ဘဲဖြစ်သွားမှာပါ။

Condition တွေကိုနှိုင်းယှဉ်ဖို့ code ရေးတဲ့အခါ python community ရဲ့ ruleတွေကို လိုက်နာပြီး line တေကို readable ဖြစ်အောင် နဲ့ရေးတာ အကောင်းဆုံးဖြစ်ပါတယ်။ ယေဘုယျအားဖြင့်ပြောရမယ်ဆိုရင် အဲ့လို python ရဲ့ rule‌တွေကို လိုက်နာပြီး ရှင်းရှင်းလင်းလင်းနဲ့ readable ဖြစ်အောင်ရေးတဲ့ နည်းစနစ်ကို pythonic လို့ခေါ်ပါတယ်။ python ရဲ့ ruleတွေက တခြား programming languageတွေထက်ပိုရှင်းပြီး လိုရင်းကိုပဲရေးဖို့လိုတာ‌ကြောင့် နားလည်ရလည်းပိုလွယ်ကူပါတယ်။

ဒီတစ်ပတ်အတွက်ကတော့ ဒီလောက်ပါပဲ။ Blog လေးကို အဆုံးထိဖတ်ပေးတဲ့အတွက် ကျေးဇူးအများကြီးတင်ပါတယ်။ နောက်weekအတွက်blogကျမှ ပြန်တွေ့ကြတာပေါ့ဗျာ။


Weekend Warriors

More from this blog

Difference Between Regional NAT Gateway and Zonal NAT Gateway

ဒီ post လေးထဲမှာတော့ Regional NAT Gateway နဲ့ Zonal NAT Gateway ကွာခြားချက်တွေကို နှိုင်းယှဉ်ပြသွားမှာဘဲ ဖြစ်ပါတယ်။ ပထဆုံးအနေနဲ့ NAT Gateway ဆိုတာ ဘာလဲ ဘာအတွက် လိုအပ်တာလဲဆိုတာကို အရင်ပြောပြပေးပါမယ်။ P

Jun 19, 20264 min read69
Difference Between Regional NAT Gateway and Zonal NAT Gateway

Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင် ဒါမျိုး Alarms လုပ် 🔥🔥🔥

High Level ရေးထားတာပါ ဒါပေမဲ့ လွယ်ပါတယ် ​ကိုယ့်မှာ AWS Infra တွေရှိတယ်ဆို တွေ့သမျှ metric တွေကို alarms တွေလုပ်ပြီး notification ယူမနေဘဲ တကယ် effective ဖြစ်တဲ့ metric တွေကိုမှ CloudWatch ရဲ့ alarm feature တွေနဲ့ ပေါင်းပြီး ပို့စေချင်ပါတယ်။ ​ဥပမာ prod...

Jan 17, 20263 min read210
Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင်  ဒါမျိုး Alarms လုပ် 🔥🔥🔥

How to connect On Premises Network and Cloud (AWS)? (Part-2)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ဘယ်လို ချိတ်ဆက်ကြမလဲ? အပိုင်း (၂) မှာ တော့ Direct connect အကြောင်းကို ဆွေးနွေး သွားမှာ ဖြစ်ပါတယ်။ အပိုင်း (၁) Site-to-site VPN အကြောင်းကို လေ့လာချင်ရင်တော့ အောက်ပါ link မှာ ...

Dec 20, 20253 min read263
How to connect On Premises Network and Cloud (AWS)? (Part-2)

How to connect On Premises Network and Cloud (AWS)? (Part-1)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ချိတ်ဆက်နိုင်တဲ့ နည်း (၂) နည်း ရှိပါတယ်။ 1. Site-to-Site VPN (Virtual Private Network) 2. Direct connect Site-to-Site VPN - On-prem network နဲ့ AWS resources တွေ ချိတ်ဆက်တဲ့...

Dec 12, 20252 min read300
How to connect On Premises Network and Cloud (AWS)? (Part-1)

Accessibility for Designer

လွန်ခဲ့တဲ့အပတ်က ရုံးက Designer တွေနဲ့ တော်ကီပွားရင်း Accessibility နဲ့ပတ်သတ်တာတွေ သူတို့ကို ရှင်းပြဖြစ်တယ်။ ကိုယ်တိုင်ကလည်း အရင်ကတည်းက ဒီ topic ကိုစိတ်ဝင်စားလို့ လေ့လာနေတာဆိုတော့ အခွင့်အရေးရရင် ရသလို sharing လုပ်ဖြစ်တယ်။ အဓိကက Accessibility နဲ့ပတ်သတ်...

Nov 21, 20253 min read85
Accessibility for Designer
M

Myanmar Technical Blog

109 posts

Cloud, Linux, DevOps, Docker, Security အစရှိတဲ့ နည်းပညာများ အကြောင်းကို မြန်မာလို ပြန်လည်မျှဝေပေးမယ့် Blog ပဲဖြစ်ပါတယ်ခဗျာ...