Conditional statements

Decision-making statement

ပထမဦးစွာ Decision-making statement များကို နားလည် သဘောပေါက်သင့်သည်။ ဆုံးဖြတ်ရမဲ့ အခြေအနေ(condition)တစ်ခုသည် မှန်သည်(True) သို့မဟုတ် မှားသည်(False)ဟူ၍ ရလဒ် နှစ်မျိုး ဖြစ်နိုင်သည်။ မှန်တယ်(True) လက်ခံနိုင်တယ် ဆိုရင် fulfilled ဖြစ်တယ်လို့ဆိုပြီးတော့လည်း သုံးနှုန် ကြပါတယ်။ မှားတယ်(False)ဆိုရင် လက်မခံနိုင်ဘူး၊ fulfilled မဖြစ်ဘူး ဆိုပြီးတော့လည်း သုံးနှုန်းကြပါတယ်။ အမှန်၊ အမှားဆုံးဖြတ်ရမဲ့ အခြေအနေ(condition)ကို စစ်ဆေးဘို့ ရေးသားထားတဲ့ code တွေကို decision-making statement လို့ခေါ်ပါတယ်။ Conditional statement လို့လည်းခေါ်ပါတယ်။ Conditional statement ဆိုတဲ့ အခေါ်အဝေါ်ကို အသုံးအများဆုံးပါ။

Python တွင် အဓိကကြသည့် control statement နှစ်မျိုးရှိသည်။

  • if control statement နှင့်
  • while control statement တို့ ဖြစ်သည်။ အတိုခေါက်အားဖြင့် If statement ဟုလည်းခေါ်သည်။

If statement ဆုံးဖြတ်ချက်တစ်ခုခုကို ချပေးရန်(decision making လုပ်ရန်)အတွက် If statement ကို အသုံးပြုသည်။ ထိုဆုံးဖြတ်ချက်မှ True သို့မဟုတ် False ကို ထုတ်ပေးသည်။ True သို့မဟုတ် False ရလဒ်ပေါ်မှုတည်၍ နောက်ထပ် ဆောင်ရွက်ရမည့် အလုပ်များကွဲပြားသွားသည်။

Python Comparison Operators
Decision-making statement များတွင် comparison operator များကို အသုံးပြုသည်။ True သို့မဟုတ် False ရလဒ် ထုတ်ပေးသည်။

Operator Meaning Example
< Less than x < y
<= Less than or equal to x <= y
== Equal to x == y
>= Greater than or equal to x >= y
> Greater than x > y
!= Not equal to x != y
In [1]:
5 != 5    # 5 is not equal to 5. --> False
Out[1]:
False

if statement

$\;\;\;\;$ if statement ၏ ရလဒ်သည် True ဖြစ်လျှင် အလုပ်ကိစ္စတချို့ကို ဆောင်ရွက်ရမည်ဖြစ်ပြီး False ဖြစ်လျှင် နောက်အဆင့်တစ်ခုသို့ ကျော်သွားရမည်။ (The if statement performs an action if a condition is True or skips the action if the condition is False.) If တစ်ခုတည်းသာပါသည့် statement နှင့် သင့်လျော်သည့် အခြေအနေများရှိသော်လည်း အသုံးနည်းသည်။ If statement တွင်

  • (၁) If တစ်ခုတည်းသာပါသည့် statement
  • (၂) If … else statement
  • (၃) If … elif … else statement
  • (၄) Nested If statement ဟူ၍ ကွဲပြားသည်။

if Statement Syntax

if ကိုရေးသည့်အခါ if နှင့် “:” တို့အကြားတွင် မိမိစစ်ဆေးလိုသည့်အချက်(test expression)ကို ထည့်ရေးရသည်။ အောက်တွင် if statement ရေးသားပုံ syntax ကို ဖော်ပြထားသည်။ If statement သည် True condition အတွက်သာ ဖြစ်သည်။ မိမိစစ်ဆေးသည့်အချက်(test expression) မှားနေလျှင်(False condition) ဘာမှလုပ်ပေးရန် မလိုသည့်အခါတွင် သုံးသည်။
$\;\;\;\;$if test expression:
$\;\;\;\;$ $\;\;\;\;$statement(s)

Python if Statement Flowchart

if_only.jpg

if statement မှ test condition သည် အခြေအနေများစွာ ဖြစ်နိုင်သည်။ ဥပမာ-ထည့်ပေးသည့် character သည် 'a' သော်လည်းကောင်း၊ 'e' သော်လည်းကောင်း၊ 'i' သော်လည်းကောင်း၊ 'o' သော်လည်းကောင်း၊ 'u' သော်လည်းကောင်း ဖြစ်လျှင် Vowel ဟု သတ်မှတ်သည်။

$\;\;\;\;$ if((character == 'a') or (character == 'e') or (character == 'i') or (character == 'o') or (character == 'u')):

and သို့မဟုတ် or စသည့် operator များ နှင့် တွဲ၍ အသုံးပြုနိုင်သည်။

if statement ကို စာကြောင်း တစ်ကြောင်းတည်းဖြင့်လည်း ဤကဲ့သို့ ရေးနိုင်သည်။
$\;\;\;\;$ if a > b: print("a is greater than b")

If ... Statement Example-1
တနင်္ဂနွေနေ့ မိုးရွာလျှင် အိမ်မှာပဲ အိပ်နေမည်။

In [2]:
weather = 'Raining'
day = 'Sunday'
if weather == 'Raining' and day == 'Sunday':
    print('Sleep at home')
Sleep at home

If ... Statement Example-2
ဤဥပမာသည် If တစ်ခုတည်းသာပါသည့် statement အတွက် အကောင်းဆုံး ဥပမာဖြစ်သည်။ True ဖြစ်လျှင်သာ အလုပ်လုပ်ရန်လိုသည်။ False ဖြစ်လျှင်ဘာမှ လုပ်ရန်မလိုပါ။
Absolute number ဟုတ် မဟုတ် စစ်ဆေးသည့် if statement ဖြစ်သည်။ ထည့်ပေးသည့် number သည် သုညထက်ငယ်လျှင် အပေါင်းကိန်း ဖြစ်အောင် ပြောင်းပေးသည်။ ထို့ကြောင့် ထည့်ပေးသည့် number သည် သုညထက် ငယ် မငယ် စစ်သည်။ True ဖြစ်လျှင် အပေါင်းကိန်း ဖြစ်အောင် ပြောင်းပေးသည်။

In [3]:
number = int(input("Please enter a number: ")) # input ကို လက်ခံ ပြီး integer အဖြစ်သို့ပြောင်းသည်။ 

if(number<0): 
    number = -(number)
print("Absolute number is: {}".format(number))
Please enter a number: -34
Absolute number is: 34

If ... Statement Example-3
If တစ်ခုတည်းသာပါသည့် ဥပမာဖြစ်သည်။ အသက်(၂၀) ပြည့်ပြီး အမျိုးသားတစ်ယောက်သည် ကျန်းမာရေးကောင်းလျှင် (၂) နှစ် စစ်မှုထမ်းရမည်။ ထိုအချက် (၃)ချက်ထဲမှ တစ်ခုခုနှင့် မကိုက်ညီပါက စစ်မှုထမ်းရန် မလိုပါ။ မကိုက်ညီလျှင် ဘာမှ လုပ်ရန်မလိုသောကြောင့် If တစ်ခုတည်းသာပါသည့် statement ကို သုံးရန်သင့်လျော်သည်။ (၃)ချက်လုံးနှင့် ကိုက်ညီရမည် ဖြစ်သောကြောင့် and ကို သုံးသည်။

In [4]:
age_above_20 = True
male = True
health_fitness_ok = True

if age_above_20 and male and health_fitness_ok : 
    print("Serve army for two years.")
Serve army for two years.

If ... Statement Example-4
အပေါင်းကိန်း ဟုတ် မဟုတ် စစ်သည့် ဥပမာ ဖြစ်သည်။

if num > 0: သည် ပေးထားသည့် ကိန်းသည် သုညထက် ကြီးသည် မကြီးသည်ကို စစ်ဆေးပြီး ဆုံးဖြတ်ချက် ချပေးသည်။ မှန်ကန်ပါက (True ဖြစ်ပါက) ထိုကိန်းသည် ပေါင်းကိန်းဖြစ်သည့်ဟု ဖော်ပြပေးသည်။ ဥပမာ တွင် ထည့်ပေးသည့်ကိန်း(၅)သည် သုညထက် ကြီးသောကြောင့် ပေါင်းကိန်းဖြစ်သည့်ဟု ဖော်ပြ ပေးသည်။ False ဖြစ်လျှင် မည်သို့ဆက်လုပ်ရမည်ကို ထည့်မရေးထားပါ။ False ဖြစ်လျှင် လုပ်ရမည့်အလုပ်ကို ရေးရန်အတွက် else… ထည့်ရေးရန်လိုသည်။ နောက်ပိုင်းတွင် If ...else Statement ဖြင့် ရေးပြထားသည်။

ဒုတိယဥပမာ တွင် ထည့်ပေးသည့်ကိန်း(၅)သည် သုညထက် ငယ်သောကြောင့် မည်သည့်အလုပ်မှ မလုပ်ပေးပါ။ အဘယ်ကြောင့်ဆိုသော် if statement မှန်ကန်လျှင်သာ လုပ်ရမည့်အလုပ်ကို ရေးထားပြီး မှားလျှင် လုပ်ပေး ရမည်အလုပ်ကို မရေးထားသောကြောင့် ဖြစ်သည်။

In [5]:
num = 5
print(num > 0)
if num > 0:          #သုညထက် ကြီးသည် မကြီးသည်ကို စစ်ဆေးသည်။
    print(num, "is a positive number.")
True
5 is a positive number.

If ... Statement Example-5
အောက်တွင် ထည့်ပေးသည့် ကိန်း(integer) တစ်ခုသည် စုံကိန်း(even number)ဖြစ်သည် သို့မဟုတ် မကိန်း(odd number)ဖြစ်သည်ကို ဆုံးဖြတ်ပေးသည်။ ပရိုဂရမ်ငယ်တစ်ခုကို ရှင်းပြထားသည်။ နံပါတ်တစ်ခုကို input အဖြစ်လက်ခံပါ။ ထိုကိန်းကို (၂) ဖြင့်စား၍ အကြွင်းမရှိလျှင် စုံကိန်း(even number)ဖြစ်သည်။ ထိုသို့မဟုတ်လျှင် မကိန်း ဖြစ်သည်။

In [6]:
num  = int(input("Enter the number: ")) # input ကို လက်ခံ ပြီး integer အဖြစ်သို့ပြောင်းသည်။ 
if num %2 ==0:                     # num ကို (၂) ဖြင့်စားရ ရသည့် ရလဒ်သည် 0 နှင့် ညီ မညီ စစ်သည်။ 
    print(num, " is even number") # True ဖြစ်လျှင် "Number is even" ဟု ဖော်ပြပေးရန် ဖြစ်သည်။ 
else:
    print(num, " is odd number") 
Enter the number: 66
66  is even number

assignment_Page_01%20addresize.jpg

If ... Statement Example-6
စာမေးပွဲအောင် မအောင် စစ်ပေးသည် ဥပမာ ဖြစ်သည်။ အမှတ် ၆၀နှင့် အထက်ရလျှင် စာမေးပွဲအောင်သည်။ If တစ်ခုတည်းသာပါသည့် statement ကိုသုံးထားသောကြောင့် True ဖြစ်လျှင်သာ အလုပ်လုပ်သည်။ False ဖြစ်လျှင် မည်သည်အလုပ်မျှ လုပ်လိမ့်မဟုတ်။ ကျော်သွားလိမ့်မည်။

In [7]:
marks = 85
if marks >= 60:      # အမှတ် ၆၀ ကျော် မကျော် စစ်သည်။ 
    print('Passed')  # အမှတ် ၆၀နှင့် အထက်ရလျှင် စာမေးပွဲအောင်သည်။ 
Passed

If ... Statement Example-7
If တစ်ခုတည်းသာပါသည့် statement ကို အသုံးပြုပြီးအငယ်ဆုံးကိန်းကို ရှာပါ။(Find the minimum of three values)။ ထည့်ပေးသည့် ကိန်း(၃)လုံးအနက်မှ အငယ်ဆုံးကိန်းကို ရှာသည့် ဥပမာ ဖြစ်သည်။

In [8]:
number1 = int(input('Enter first integer: '))
number2 = int(input('Enter second integer: '))
number3 = int(input('Enter third integer: '))

minimum = number1
if number2 < minimum:
    minimum = number2

if number3 < minimum:
    minimum = number3

print('Minimum value is', minimum)
Enter first integer: 12
Enter second integer: 45
Enter third integer: 33
Minimum value is 12

If ... Statement Example-8

In [9]:
gender = 'Female'
age = 70
if gender == 'Female' and age >= 65:
    print('Senior female')
Senior female

if... else (elif) statement

$\;\;\;\;$အခြေအနေတစ်ခုသည် True ဖြစ်လျှင် အလုပ်ကိစ္စတချို့ကို ဆောင်ရွက် ရမည်ဖြစ်ပြီး False ဖြစ်လျှင် တခြားအလုပ်ကိစ္စတချို့ကို ဆောင်ရွက် ရမည်ဖြစ်သည်။(The if... else statement performs an action if a condition is True or performs a different action if the condition is False.)

Test expression မှထွက်လာသည့် ရလဒ် နှစ်ခုစလုံးတွက် သုံးလိုသည့်အခါ If..else statement ကိုသုံးသည်။ Test expression ၏ ရလဒ်သည် True ဖြစ်လျှင်(မှန်လျှင်) If အောက်ရှိ statement အတိုင်း လုပ်ပါ။ မှားလျှင်(False ဖြစ်လျှင်) else: အောက်က statement အတိုင်းလုပ်ပါ။

Python if…else Statement Syntax

if test expression:
$\;\;\;\;$ statement(s)
else:
$\;\;\;\;$ statement(s)

Python if…else Statement Flowchart if%20else.jpg

If ...else ... Statement Example-1
စုံကိန်းလား? မကိန်းလား? စစ်ပေးသည် ဥပမာ ဖြစ်သည်။

In [10]:
number = int(input("Please Enter a number: "))
if(number % 2 ==0):
    print("Even Number")
else:
    print("Odd Number")
Please Enter a number: 12
Even Number

If ...else Statement Example-2
အပေါင်းကိန်းလား? အနုတ်ကိန်းလား? (Positive or Negative number) စစ်ပေးသည် ဥပမာ ဖြစ်သည်။

assignment_Page_04%20and%205_resize.jpg

In [11]:
number = int(input("Please Enter a number: "))
if(number>0):
    print(number , "is positive number.")
else:
    print(number , "is negative number.")
Please Enter a number: 099
99 is positive number.

If ...else ... Statement Example-3
မဲပေးဘို့ အသက်ပြည့်ပြီလား?(eligible to vote)

In [12]:
age = int(input("Enter your age? "))
if age >=18:
    print("your are eligible to vote")
else:
    print("Sorry!! you have to wait!!!")
Enter your age? 17
Sorry!! you have to wait!!!

If ...else ... Statement Example-4
စာမေးပွဲ အောင်လား? ကျလား?

In [13]:
grade = 85
if grade >= 60:
    print('Passed')
else:
    print('Failed')
Passed

If ...else ... Statement Example-5
သရအက္ခရာလား ဗျည်းအက္ခရာလား(vowel or constant ?) Logical test သည် တစ်ခုထက်ပိုများသည့် အခြေအနေများဖြစ်နိုင်သည်။ အောက်တွင် logical test သည် တစ်ခုချင်း စစ်နိုင်သလို သင့်လျော်သလို ပြင်ဆင်ပြီး စစ်စရာလိုသည့် logical test များကို အရေအတွက်နည်းသွားအောင်လျော့ချနိုင်သည်။
a, e, i, o, u စသည့် သရအက္ခရာများကို တစ်လုံးချင်း “==” ဖြင့်စစ်မည့်အစား a, e, i, o, u တို့ကို list တစ်ခုတည်ဆောက်၍ “in” operator သုံးပြီး logical test တစ်ခုတည်းဖြင့် စစ်နိုင်ပုံကို ဖော်ပြထားသည်။

In [14]:
input_char = str(input("Please Enter a Character: "))

if((input_char== 'a') or (input_char== 'A') or (input_char== 'e') or (input_char== 'E') or (input_char== 'i')
   or (input_char== 'I') or (input_char== 'o') or (input_char== 'O') or (input_char== 'u') or (input_char== 'U')):
    print("Vowel")
else:
    print("Consonant")
Please Enter a Character: E
Vowel
In [15]:
#a, e, i, o, u စသည့် သရအက္ခရာများကို တစ်လုံးချင်း “==” ဖြင့်စစ်မည့်အစား a, e, i, o, u တို့ကို list တစ်ခုတည်ဆောက်၍
#“in” operator သုံးပြီး logical test တစ်ခုတည်းဖြင့် စစ်နိုင်သည်။
input_char = str(input("Please Enter a Character: "))
vowel = [ 'a', 'e', 'i','o','u', 'A', 'E', 'I', 'O', 'U']
if(input_char in vowel):
    print("Input character is Vowel.")
else:
    print("Input character is Consonant.")
Please Enter a Character: e
Input character is Vowel.

assignment_Page_10%20resize400.jpg

If ...else ... Statement Example-6
ထည့်ပေးသည့် ကိန်းနှစ်လုံးမှ ငယ်သည့်ကိန်းကို ရွေးပါ။

In [16]:
number1 = int(input("Please Enter First Number: "))
number2 = int(input("Please Enter Second Number: "))

if(number1 < number2):
    print("Less Number is: {}".format(number1))
else:
    print("Less Number is: {}".format(number2))
Please Enter First Number: 53
Please Enter Second Number: 23
Less Number is: 23

If ...else ... Statement Example-7
ထည့်ပေးသည့် ကိန်းနှစ်လုံးမှ ငယ်သည့်ကိန်းကို ရွေးပါ။

In [17]:
marks = int(input("Enter the marks?"))
if marks >=85 and marks <=100:
    print("Congrats1 your scored grade A...")
elif marks >60 and marks <=85:
    print("you scored grade B+...")
elif marks >40 and marks <=60:
    print("you scored grade B...")
elif marks >30 and marks <=40:
    print("you scored grade C...")
else:
    print("Sorry you have failed")
Enter the marks?75
you scored grade B+...

if... elif... else statement

The if... elif... else statement performs one of many different actions, depending on the truth or falsity of several conditions စစ်ရန်အခြေအနေ()နှင့် လုပ်ဆောင်ရန် ကိစ္စများစွာ ပါဝင်သည့်အခါ if... elif... else statement ကို အသုံးပြုသည်။ တချို့သော လက်တွေ့ အခြေအနေများသည် အနည်းငယ် ရှုပ်ထွေးသောကြောင့် ဒါဖြစ်လျှင် ဒါလုပ်ရမည့် ဆိုသည့် ပုံသေပြောရန် မဖြစ်နိုင်ပါ။ ထို့ကြောင့် if... elif... else သည် လက်တွေ့တွင် အသုံးများသည်။
if..elif..else Statement တွင်
$\;\;\;\;$ (၁) ပထမဦးစွာ if statement အတွက် condition တစ်ခုကို စစစ်သည်။ True ဖြစ်လျှင် if statement အောက်က ကိစ္စကို ဆောင်ရွက်(execute)သည်။ False ဖြစ်လျှင် elif statement ကို စစ်သည်။
$\;\;\;\;$ (၂) elif statement မှ ရလဒ်သည် True ဖြစ်လျှင် elif statement အောက်က ကိစ္စကို ဆောင်ရွက် (execute)သည်။ False ဖြစ်လျှင် else statement မှ ဆီသို့ရောက်သည်။

Syntax of if..elif..else statement

ပထမ test expression မှန်လျှင် အောက်က အတိုင်းလုပ်ပါ။ မှားလျှင် နောက်ထပ် test expression တစ်ခုကို elif: ဖြင့် ထပ်စစ်ပြီး မှန်လျှင် elif: အောက်က statement အတိုင်းလုပ်ပါ။

Python if…else Statement Syntax

if test expression:
$\;\;\;\;$ statement(s)
elif test expression:
$\;\;\;\;$ statement(s)
else test expression:
$\;\;\;\;$ statement(s)

Python if..elif..else Statement Flowchart

if%20elif%20else.jpg

If elif else Statement Example-1
အောက်တွင် ထည့်ပေးသည့် ကိန်း(integer) တစ်ခုသည် စုံကိန်း(even number)ဖြစ်သည် သို့မဟုတ် မကိန်း(odd number)ဖြစ်သည်ကို ဆုံးဖြတ်ပေးသည့် အပေါ်က ပရိုဂရမ်ကို ပြည့်စုံအောင် condition တစ်ခုထည့်ဖြည့်ထားသည်။ သုည(Zero)သည် စုံကိန်း(even number)လည်းမဟုတ် မကိန်း(odd number) မဟုတ်သောကြောင့် အခြေအနေ တစ်ခုထပ်ဖြည့်ရန်လိုသည်။ ထို့ကြောင့် If elif else statement ကိုသုံး၍ ဖော်ပြထားသည်။

In [18]:
num = int(input('Enter a number: '))
if num > 0:
    print('The number is positive')
elif num < 0:
    print('The number is negative')
else:
    print('The number is zero')
Enter a number: 86
The number is positive

If elif else Statement Example-2
ရက်ထပ်နှစ်လား? (Leap Year?)
ခုနှစ်ကို (၄)နှင့်စား၍ ပြတ်လျှင် ရက်ထပ်နှစ်ဖြစ်သည်။

In [19]:
year = int(input("Please enter Year: "))

if(year%100 != 0 ) and (year%4 ==0):
    print("leap Year")
elif(year % 100 == 0) and (year % 400 == 0):
    print("leap Year")
else:
    print("Not Leap Year")
Please enter Year: 2020
leap Year

If elif else Statement Example-3
သစ်သီးလား ဟင်းသီးဟင်းရွက်လား? (fruit or vegetable?)
သစ်သီးလား ဟင်းသီးဟင်းရွက်လား စစ်ဆေးသည့် ဥပမာဖြစ်သည်။

In [20]:
# Test expression တွင် test condition များစွာပါသည်။ 
a = str(input())
if a == 'banana' or a == 'apple' or a == 'kiwi' or a == 'cherry' or a == 'lemon' or a == 'grapes':
    print(a , " is fruit.")
elif a == 'tomato' or a == 'cucumber' or a == 'pepper' or a == 'carrot':
    print(a ," is vegetable.")
else:
    print("unknown")
banana
banana  is fruit.
In [21]:
# Test expression တွင် test condition တစ်ခုသာ ပါသည်။ 
a = str(input())
fruit = [ 'banana' , 'apple', 'kiwi' , 'cherry' , 'lemon' , 'grapes']
vegetable = [ 'tomato',  'cucumber' , 'pepper' , 'carrot' ]
if a in fruit:
    print(a , " is fruit.")
elif a in vegetable:
    print(a ," is vegetable.")
else:
    print("unknown")
tomato
tomato  is vegetable.

If elif else Statement Example-3
Weekend နှင့် Working day ခွဲခြားသည့် ဥပမာ

In [22]:
a = str(input())
if a == 'Monday' or a == 'Tuesday' or a == 'Wednesday' or a == 'Thursday' or a == 'Friday':
    print("Working day")
elif a == 'Saturday' or a == 'Sunday':
    print("Weekend")
else:
    print("Error")
Sunday
Weekend

If elif else Statement Example-4
Comparing integers using if statements and comparison operators.

In [23]:
print('Enter two integers, and I will tell you','the relationships they satisfy.')
# read first integer
number1 = int(input('Enter first integer: '))
# read second integer
number2 = int(input('Enter second integer: '))
if number1 == number2:
    print(number1, 'is equal to', number2) 
if number1 != number2:
    print(number1, 'is not equal to', number2)
    
if number1 < number2:
    print(number1, 'is less than', number2)
if number1 > number2:
    print(number1, 'is greater than', number2)
    
if number1 <= number2:
    print(number1, 'is less than or equal to', number2)
if number1 >= number2:
    print(number1, 'is greater than or equal to', number2)
Enter two integers, and I will tell you the relationships they satisfy.
Enter first integer: 77
Enter second integer: 12
77 is not equal to 12
77 is greater than 12
77 is greater than or equal to 12

If elif else Statement Example-5
ရုပ်ရှင်ရုံဝင်ကြေးလက်မှတ်ခသည် နေ့ကိုလိုက်၍ ပြောင်းလဲသည်။ တနင်းလာနေ့၊ အင်္ဂါ နေ့ နှင့် သောကြာများတွင် ဈေးသက်သာသည်။(2000ကျပ်)။ ဗုဒ္ဓဟူးနေ့နှင့် ကြာပတေးနေ့တွင် ဈေးအသင့်အတင့်ဖြစ်သည်။(4000ကျပ်)။ စနေ့နေ့နှင့် တနင်္ဂနွေနေများတွင် ဈေးကြီးသည်။ (6000ကျပ်)။ စသည့်နှုန်းများအတွက် တွက်ပေးသည့် if elif statement ကို အောက်တွင် ဖော်ပြထားသည်။

In [24]:
a = str(input())
if a == 'Monday' or a == 'Tuesday' or a == 'Friday':
    print(2000)
elif a == 'Wednesday' or a == 'Thursday':
    print(4000)
elif a == 'Saturday' or a == 'Sunday':
    print(6000)
Sunday
6000

If elif else Statement Example-6
checking for prime number

In [25]:
# checking for prime number
number = int(input("Enter any number: "))

# prime is always greater than 1
if number > 1:
    for i in range(2, number):
        if (number % i) == 0:
            print(number, " is not a prime number")
            break
    else:
        print(number, "is a prime number")
# if the entered number is less than or equal to 1, then it is not prime number
else:
    print(number, "is not a prime number")
Enter any number: 3
3 is a prime number

Python Nested if statements

if statement တစ်ခုအတွင်း၌ တခြားသော if statement များကို ထည့်သွင်းရေးသားနိုင်သည်။ ထိုသို့ if statement များ အထပ်ထပ်ရေးထားခြင်းကို Nested if statement ဟုခေါ်သည်။ အောက်တွင် Nested if statement ဥပမာ တစ်ခုကို ဖော်ပြထားသည်။

Nested if statements Example-1
အောက်တွင် အရောင်းဝန်ထမ်းတစ်ယောက် ရရှိမည့် ကော်မရှင် ပမာဏကို တွက်ပေးမည့် ဥပမာကို ရှင်းပြထားသည်။ ရောင်းသည့် မြို့ကို လိုက်၍ ကော်မရှင်ရာခိုင်နှုန်း ကွဲပြားသည်။ ရောင်းအားမှုတည်၍ ကော်မရှင်ရာခိုင်နှုန်း ကွဲပြားသည်။

  • ရန်ကုန်တိုင်း သို့မဟုတ် မန္တလေးတိုင်း အတွက် ရောင်းအား ၅သိန်းနှင့် အောက်ဖြစ်လျှင် ၅% ဖြစ်သည်။ ၅သိန်းနှင့် ၁၀သိန်းအကြားတွင် ၇% ဖြစ် စသည်ဖြင့် အသီးသီးကွဲပြားသည်။
  • ကျန်ဒေသများအတွက် ရောင်းအား ၅သိန်းနှင့် အောက်ဖြစ်လျှင် ၅.၅% ဖြစ်သည်။ ၅သိန်းနှင့် ၁၀သိန်းအကြားတွင် ၈% ဖြစ် စသည်ဖြင့် အသီးသီးကွဲပြားသည်။
    ပထမဦးစွာ if statement ဖြင့် Yangon or Mandalay ခွဲသည်။ ထို့နောက် ဖြင့် ကော်မရှင် ပမာဏ အသီးသီးကို တွက်သည်။
In [26]:
city = str(input())
sales = float(input())

if city == 'Yangon' or city == 'Mandalay'  : 
    if 0 <= sales <= 500000:
        print(f'{(sales * 0.05):.2f}')
    elif 500 < sales <= 1000000:
        print(f'{(sales * 0.07):.2f}')
    elif 1000 < sales <= 10000000:
        print(f'{(sales * 0.08):.2f}')
    elif sales > 10000000:
        print(f'{(sales * 0.12):.2f}')
    else:
        print("error")
        
elif city == 'other':
    if 0 <= sales <= 500:
        print(f'{(sales * 0.055):.2f}')
    elif 500 < sales <= 1000:
        print(f'{(sales * 0.08):.2f}')
    elif 1000 < sales <= 10000:
        print(f'{(sales * 0.12):.2f}')
    elif sales > 10000:
        print(f'{(sales * 0.145):.2f}')
    else:
        print("error")
else:
    print("error")
Yangon
9300000
744000.00

Nested if statements Example-2
ကုန်တင်မောင်းသူများ၏ ဝင်ငွေ(ဒရိုင်ဘာကြေး) သည် မောင်းသည့် ရာသီနှင့် အကွာအဝေးပေါ်မူတည်သည်။

  • 'Spring' သို့မဟုတ် 'Autumn' တွင် တစ်လလျှင် ကီလိုမီတာ 5000 အောက်မောင်းပါက ထိုလ ဝင်ငွေသည် (km_per_month = 0.75 4 0.9) ဒေါ်လာ ဖြစ်သည်။
  • 5000 နှင် 10000 အကြားမောင်းခဲ့လျှင် ထိုလဝင်ငွေသည် (km_per_month = 0.95 4 0.9) ဒေါ်လာ ဖြစ်သည်။ 'Summer' နှင့် 'Winter' တို့တွင် တွက်သည့်နှုန်းများမတူကြပေ။ စသည်ဖြင့် ရာသီနှင့် အကွာအဝေး အသီးသီးတွက်ယူပုံကို Nested if statement သုံးထားသည်။
In [27]:
season = str(input())
km_per_month = float(input())

if season == 'Spring' or season == 'Autumn':
    if km_per_month <= 5000:
        km_per_month *= 0.75 * 4 * 0.9
    elif 5000 < km_per_month <= 10000:
        km_per_month *= 0.95 * 4 * 0.9
    elif 10000 < km_per_month <= 20000:
        km_per_month *= 1.45 *4 * 0.9
        
elif season == 'Summer':
    if km_per_month <= 5000:
        km_per_month *= 0.9 * 4 * 0.9
    elif 5000 < km_per_month <= 10000:
        km_per_month *= 1.1 * 4 * 0.9
    elif 10000 < km_per_month <= 20000:
        km_per_month *= 1.45 * 4 * 0.9
        
elif season == 'Winter':
    if km_per_month <= 5000:
        km_per_month *= 1.05 * 4 * 0.9
    elif 5000 < km_per_month <= 10000:
        km_per_month *= 1.25 * 4 * 0.9
    elif 10000 < km_per_month <= 20000:
        km_per_month *= 1.45 * 4 * 0.9
        
print(f'{km_per_month:.2f}')
Winter
11000
57420.00
In [ ]:
-End-