Professional Documents
Culture Documents
If Statement
If Statement
Contents
10.2 if Statement...................................................................................................................... 4
10-1
Chapter-10 Conditional Statements
10.4.3 If elif else Statement Example-3 ............................................................................. 14
10-2
ကကောင််းထက်ညွန် Conditional Statements
5 != 5 # 5 is not equal to 5.
False
10.1.2 Python Logical Operators
and Returns True if both statements are true x < 5 and x < 10
10-3
Chapter-10 Conditional Statements
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
Object တစ်ြေိုသည် သလိုသညဲ့် sequence ထတွင် ပေါ င်ကနသည် မပေါ င်သည်ကို သရန်အတွက်
membership operator ကို အသို်းဖပ သည်။
False
Python if Statement Syntax Test Expression
if test expression:
True
statement(s)
Body of If Statement
if Statement Syntax
if ကက
ို ရ်းသညဲ့်အြေေါ if နှငဲ့် “:” တအကကော်းတွ
ို င် မမ စစ်ကဆ်းလိုသညဲ့အ
် ြေ က်(test expression)ကို
ထညဲ့်ကရ်း ရသည်။ ကအောက်တင
ွ ် if statement ကရ်းသော်းပို syntax ကို ကြော်ဖပထော်းသည်။ If statement သည်
10-4
ကကောင််းထက်ညွန် Conditional Statements
True condition အတွက်သော ဖြစ်သည်။ မမ စစ်ကဆ်းသညဲ့်အြေ က်(test expression)သည် မှော်းကနလျှင်(False
condition) ောမှ လိုပ်ကပ်းရန် မလိုသညဲ့်အြေေါတွင် သို်းသည်။
ဥပမော-ထညဲ့်ကပ်းသညဲ့် character သည် 'a' ကသော်လည််းကကောင််း၊ 'e' ကသော်လည််းကကောင််း၊ 'i'
ကသော်လည််းကကောင််း၊ 'o' ကသော်လည််းကကောင််း၊ 'u' ကသော်လည််းကကောင််း ဖြစ်လျှင် သရ(vowel) ဟို
သတ်မှတ်သည်။ Test condition (၅)ြေိုကို or operator ဖြငဲ့် ဆက်ထော်းသည်။
and သမဟို
ို တ် or စသညဲ့် logical operator မ ော်း နှငဲ့် တွ၍ အသို်းဖပ နိုငသ
် ည်။ if statement ကို
စောကကကောင််း တစ်ကကကောင််းတည််းဖြငဲ့်လည််း ဤကဲ့သို ကရ်းနိုင်သည်။ သကသော်
ို ြတ်ရန် မလွယက
် ူပေါ။
If today == user.birthday
Print (“Happy Birthday”)
weather = 'Raining'
day = 'Sunday'
if weather == 'raining' and day == 'Sunday':
print('Sleep at home')
Sleep at home
10.2.4 If ... Statement Example-3
10-5
Chapter-10 Conditional Statements
number = -(number) # အကပေါင််းကန််းဖြစ်ကအောင် ကဖပောင််းသည်။
print("Absolute number is: {}".format(number))
Please enter a number: -34
Absolute number is: 34
10.2.5 If ... Statement Example-4
age_above_20 = True
male = True
health_fitness_ok = True
ဥပမောတွင် ထညဲ့်ကပ်းသညဲ့က
် န််း(5)သည် သိုညထက် ငယ်ကသောကကကောငဲ့် မည်သညဲ့အ
် လိုပ်မှ မလိုပ်
ကပ်းပေါ။ အ ယ်ကကကောငဲ့်ဆိုကသော် if statement မှနက
် န်လျှငသ
် ော လိုပ်ရမညဲ့အ
် လိုပက
် ို ကရ်းထော်းပပ်း မှော်းလျှင်
လိုပ်ကပ်းရမည်ဲ့ အလိုပက
် ို မကရ်းထော်းကသောကကကောငဲ့် ဖြစ်သည်။
num = 5
print(num > 0)
if num > 0: # သိုညထက် ကက်းသည် မကက်းသည်ကို စစ်ကဆ်းသည်။
10-6
ကကောင််းထက်ညွန် Conditional Statements
print(num, "is a positive number.")
True
5 is a positive number.
10.2.7 If ... Statement Example-6
Odd
End
marks = 85
if marks >= 60: # အမှတ်(၆၀)ကက ော် မကက ော် စစ်သည်။
10-7
Chapter-10 Conditional Statements
Passed
10.2.9 If ... Statement Example – 8
If တစ်ြေတ
ို ည််းသောပေါသညဲ့် statement ကို အသို်းဖပ ပပ်း ထညဲ့်ကပ်းသညဲ့် ကန််း(၃)လို်း အနက်မှ
အငယ်ဆို်း ကန််းကို ရှောသညဲ့် ဥပမော ဖြစ်သည်။
အသက်(၆၅)နှစ် သမဟို
ို တ် (၆၅)နှစက
် က ော်လျှင်(age >= 65) အမ ်းသမ်းဖြစ်လျှင(် gender ==
'Female') အြွော်း(senior female)ဟို သတ်မှတ်သည်။ အြေ က် နှစြေ
် က်စလို်းနှငဲ့် ဖပညဲ့စ
် ိုရမည် ဖြစ်ကသော
ကကကောငဲ့် and operator ကို သို်းသည်။
gender = 'Female'
age = 70
if gender == 'Female' and age >= 65:
print('Senior female')
Senior female
10.2.11 If ... Statement Example – 10
စက
ို န််းလော်း? မကန််းလော်း? စစ်ကပ်းသည် ဥပမော ဖြစ်သည်။
အကပေါင််းကန််းလော်း? အနိုတက
် န််းလော်း? (positive or negative number) စစ်ကပ်းသည် ဥပမော ဖြစ်သည်။
grade = 85
if grade >= 60:
print('Passed')
else:
print('Failed')
Passed
10-10
ကကောင််းထက်ညွန် Conditional Statements
10.3.5 If ...else ... Statement Example - 5
(၁) ပထမဦ်းစွော if statement အတွက် condition တစ်ြေိုကို စစစ်သည်။ True ဖြစ်လျှင် if statement
ကအောက်က ကစစကို ကဆောင်ရက
ွ ်(execute)သည်။ False ဖြစ်လျှင် elif statement ကို စစ်သည်။
(၂) elif statement မှ ရလဒ်သည် True ဖြစ်လျှင် elif statement ကအောက်က ကစစကို ကဆောင်ရွက်
(execute)သည်။ False ဖြစ်လျှင် else statement မှ ဆသကရောက်
ို သည်။
if test expression:
statement(s)
Body of If Statement
True
Body of elif Body of else
10-12
ကကောင််းထက်ညွန် Conditional Statements
Start
Input a
year
ရက်ထပ်နှစလ
် ော်း? (Leap year?) ြေိုနှစက
် ို (၄)နှငဲ့်စော်း၍ ဖပတ်လျှင် ရက်ထပ်နစ
ှ ် ဖြစ်သည်။
10-13
Chapter-10 Conditional Statements
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.
if a in fruit:
print(a , " is fruit.")
elif a in vegetable:
print(a ," is vegetable.")
else:
print("unknown")
tomato
tomato is vegetable.
10-14
ကကောင််းထက်ညွန် Conditional Statements
10.4.4 If elif else Statement Example-4
a = str(input())
if a == 'Monday' or a == 'Tuesday' or a == 'Wednesday' or a == 'Thur
sday' or a == 'Friday':
print("Working day")
elif a == 'Saturday' or a == 'Sunday':
print("Weekend")
else:
print("Error")
Sunday
Weekend
else:
print("Sorry you have failed")
Enter the marks?75
you scored grade B+...
10.4.6 If elif else Statement Example - 6
ရိုပ်ရှငရ
် ို င်ကကက်းလက်မှတ်ြေသည် ကနကိုလက
ို ်၍ ကဖပောင််းလသည်။ တနင််းလောကန၊ အင်္ဂေါ ကန နှငဲ့်
ကသောကကော ကနမ ော်းတွင် က ်းသက်သောသည်။(2000က ပ်)။ ဗိုဒဟ
ဓ ူ်းကနနှငဲ့် ကကောပကတ်းကနတွင် က ်းအသငဲ့အ
် တငဲ့်
ဖြစ်သည်။ (4000က ပ်)။ စကနကနနှငဲ့် တနင်္ဂကနွကနမ ော်းတွင် က ်းကက်းသည်။ (6000က ပ်)။ စသညဲ့် နှုန််းမ ော်း
10-15
Chapter-10 Conditional Statements
အတွက် တွကက
် ပ်းသညဲ့် if elif statement ကို ကအောက်တင
ွ ် ကြော်ဖပထော်းသည်။ ရက်အမည်ကို string ဖြငဲ့်
ထညဲ့်ကပ်းပေါက ရိုပ်ရှငလ
် က်မတ
ှ ်ကစ ်းနှုန််းကို တွက်ကပ်းသညဲ့် ဥပမော ဖြစ်သည်။
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
10.4.7 If elif else Statement Example-7
if statement တစ်ြေအ
ို တွင််း၌ တဖြေော်းကသော if statement မ ော်းကို ထညဲ့်သွင််းကရ်းသော်းနိုင်သည်။ ထသ
ို ို
if statement မ ော်း အထပ်ထပ်ကရ်းထော်းဖြေင််းကို Nested if statement ဟိုကြေေါ်သည်။ ကအောက်တွင် Nested if
statement ဥပမော တစ်ြေိုကို ကြော်ဖပထော်းသည်။
10-16
ကကောင််းထက်ညွန် Conditional Statements
• ရန်ကန
ို ်တိုင််း သမဟို
ို တ် မနတကလ်းတိုင််း အတွက် ကရောင််းအော်း ၅သန််းနှငဲ့် ကအောက်ဖြစ်လျှင် ၅% ဖြစ်သည်။
၅သန််းနှငဲ့် ၁၀သန််းအကကော်းတွင် ၇% ဖြစ် စသည်ဖြငဲ့် အသ်းသ်းကွဖပော်းသည်။
• က န်ကဒသမ ော်းအတွက် ကရောင််းအော်း ၅သန််းနှငဲ့် ကအောက်ဖြစ်လျှင် ၅.၅% ဖြစ်သည်။ ၅သန််းနှငဲ့် ၁၀သန််း
အကကော်းတွင် ၈% ဖြစ် စသည်ဖြငဲ့် အသ်းသ်းကွဖပော်းသည်။
ပထမဦ်းစွော if statement ဖြငဲ့် Yangon, Mandalay နှငဲ့် က န်ပမ ျို့မ ော်းကို ြေွသည်။ ထိုကန က်
ကရောင််းအော်းဖြငဲ့် ကကော်မရှငပ
် မောဏ အသ်းသ်းကို တွက်သည်။
city = str(input())
sales = float(input())
ကိုနတ
် င်ကမောင််းသူမ ော်း၏ င်ကငွ(ဒရိုင် ောကကက်း)သည် ကမောင််းရသညဲ့် ရောသ(ကနွ, မို်း, ကဆောင််း)နှငဲ့်
အကွောအက ်း အကပေါ် မူတည်သည်။
10-17
Chapter-10 Conditional Statements
• 'Spring' သမဟို
ို တ် 'Autumn' တွင် တစ်လလျှင် ကလမ
ို တော 5000 ကအောက်ကမောင််းပေါက ထလ
ို င်ကငွသည်
(km_per_month = 0.75 *4 * 0.9) ကဒေါ်လော ဖြစ်သည်။
season = str(input())
km_per_month = float(input())
print(f'{km_per_month:.2f}')
Winter
11000
57420.00
-End-
10-18