Thuta Learning
Python
BasicProgrammingအခြေခံ

Python Booleans

Pythonသင်ခန်းစာ 1120 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Comparison operator တွေက True/False ပြန်ပေးပုံကို သုံးပြီး condition ရေးနိုင်ဖို့
  • and, or, not ရဲ့ ရလဒ်ကို ကြိုခန့်မှန်းပြီး ကွင်းနဲ့ အစီအစဉ် ထိန်းနိုင်ဖို့
  • 0, "", [], None လို falsy value တွေကို သိပြီး if items: လို ရေးနိုင်ဖို့
  • role == "admin" or "editor" လို အမြဲ True ဖြစ်နေတဲ့ bug ကို ရှာဖွေ ပြင်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Boolean ဆိုတာ "ဟုတ်လား၊ မဟုတ်ဘူးလား" ဆိုတဲ့ မေးခွန်းရဲ့ အဖြေပါ။ Value နှစ်ခုပဲ ရှိပါတယ် — True နဲ့ False။ Python မှာ T နဲ့ F ကို စာလုံးအကြီးနဲ့ ရေးရပြီး quote မပါပါဘူး၊ "True" လို quote နဲ့ ရေးရင် string ဖြစ်သွားပါတယ်။

Program တွေက ဆုံးဖြတ်ချက်ချတိုင်း boolean ကို သုံးပါတယ်။ Password မှန်လား၊ cart ထဲမှာ ပစ္စည်း ရှိလား၊ user က admin လား — ဒီမေးခွန်းတွေရဲ့ အဖြေက True/False ဖြစ်ပြီး if statement က အဲဒီ အဖြေပေါ် မူတည်ပြီး ဘယ်လမ်းကြောင်း သွားမလဲ ရွေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: viewer တစ်ယောက်က edit button ကို မြင်နေရတယ်

Admin နဲ့ editor တွေကိုပဲ Edit button ပြချင်လို့ အောက်က condition ကို ရေးလိုက်ပါတယ်။ အင်္ဂလိပ်စာလို ဖတ်ကြည့်ရင် "role က admin ဒါမှမဟုတ် editor ဖြစ်ရင်" လို့ မှန်နေသလို ထင်ရပါတယ်။ ဒါပေမယ့် role က "viewer" ဖြစ်နေတာတောင် button ပေါ်နေပါတယ်၊ ဘယ် role ပဲ ဖြစ်ဖြစ် condition က True ပါပဲ။

permissions.py
role = "viewer"

if role == "admin" or "editor":     # always True!
    print("Show Edit button")

# Python reads it as: (role == "admin") or ("editor")
#                     False             or  "editor"  -> "editor" (truthy)

ဘာကြောင့် အမြဲ True ဖြစ်နေတာလဲ

or က နှိုင်းယှဉ်ချက် နှစ်ခုကို ချိတ်တာ မဟုတ်ဘဲ သီးခြား expression နှစ်ခုကို ချိတ်တာပါ။ ဘယ်ဘက် role == "admin" က False ဖြစ်တော့ Python က ညာဘက်ကို ကြည့်ပြီး "editor" ဆိုတဲ့ string ကို ရပါတယ်။ ဗလာမဟုတ်တဲ့ string တိုင်းက truthy ဖြစ်လို့ if က အမြဲ ဝင်ပါတယ်။ မှန်ကန်တဲ့ ရေးနည်းက role == "admin" or role == "editor" ဒါမှမဟုတ် role in ("admin", "editor") ပါ။

Core concept: comparison, logic နဲ့ truthiness

Boolean value တွေ အများဆုံး ထွက်လာတဲ့ နေရာက comparison တွေပါ။ ==၊ !=၊ >၊ <၊ >=၊ <= တို့က ဘယ်ဘက်နဲ့ ညာဘက်ကို နှိုင်းယှဉ်ပြီး True ဒါမှမဟုတ် False ပြန်ပေးပါတယ်။ = (assign) နဲ့ == (နှိုင်းယှဉ်) ကို မရောပါနဲ့။ Python မှာ 1 < x < 10 လို comparison တွေကို ဆက်ရေးလို့ရပြီး 1 < x and x < 10 နဲ့ အဓိပ္ပာယ် တူပါတယ်။

OperatorTrue ဖြစ်တဲ့ အခါဥပမာ
a and bနှစ်ခုလုံး truthy ဖြစ်မှTrue and False → False
a or bအနည်းဆုံး တစ်ခု truthy ဖြစ်ရင်True or False → True
not aa က falsy ဖြစ်ရင်not True → False

Python ရဲ့ value တိုင်းမှာ truthiness ရှိပါတယ်၊ ဆိုလိုတာက if ထဲ ထည့်လိုက်ရင် True လို ဒါမှမဟုတ် False လို ပြုမူပါတယ်။ Falsy value တွေက နည်းနည်းပဲ ရှိပါတယ် — False၊ None၊ 0၊ 0.0၊ ဗလာ string ""၊ ဗလာ list []၊ ဗလာ tuple ()၊ ဗလာ dict {} နဲ့ ဗလာ set တို့ပါ။ ကျန်တာ အားလုံး truthy ဖြစ်လို့ " " (space တစ်ခု) နဲ့ [0] တောင် True ပါ။ bool(value) က အဲဒီ truthiness ကို တိုက်ရိုက် ပြပေးပါတယ်။

and နဲ့ or က short-circuit လုပ်ပါတယ်။ and ရဲ့ ဘယ်ဘက်က falsy ဆိုရင် ညာဘက်ကို လုံးဝ မတွက်တော့ဘဲ ဘယ်ဘက် value ကို ပြန်ပေးပါတယ်၊ or ရဲ့ ဘယ်ဘက်က truthy ဆိုရင်လည်း ဒီအတိုင်းပါပဲ။ ဒါကြောင့် and/or က True/False အမြဲ ပြန်တာ မဟုတ်ဘဲ operand တစ်ခုခုကို ပြန်ပေးတာပါ — nickname or "Guest" က nickname ဗလာဆိုရင် "Guest" ကို ပြန်ပေးပါတယ်။ Priority အစဉ်က not အရင်၊ ပြီးရင် and၊ နောက်ဆုံး or ပါ၊ ရှုပ်လာရင် ကွင်းခတ်ပါ။

နောက်ဆုံး အချက်တစ်ခုက bool ဟာ int ရဲ့ subclass ဖြစ်တာပါ။ True က 1 နဲ့ ညီပြီး False က 0 နဲ့ ညီလို့ True + True က 2 ရပါတယ်။ ဒီအချက်ကြောင့် sum() နဲ့ condition တစ်ခု ဘယ်နှကြိမ် True ဖြစ်လဲ ရေတွက်လို့ ရပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: if က truthiness ကို မေးတယ်
  1. Comparison က True/False ထုတ်
  2. Value တိုင်းမှာ truthy/falsy ရှိ
  3. and/or က ဘယ်ကနေ ညာ short-circuit
  4. if က ရလဒ်ရဲ့ truthiness ကို စစ်

if က True ကို တိုက်ရိုက် မလိုအပ်ပါဘူး၊ truthy ဖြစ်တဲ့ ဘယ် value မဆို လုံလောက်ပါတယ် — ဒါက အသုံးဝင်သလို bug ရဲ့ အရင်းအမြစ်လည်း ဖြစ်ပါတယ်။

အထွေထွေ ပုံစံ

python
is_ok = left == right            # comparison -> bool
in_range = low <= value < high   # chained comparison

both = condition_a and condition_b
either = condition_a or condition_b
opposite = not condition

if collection:                   # truthy when not empty
    ...
result = value or default        # first truthy operand

Comparison တွေက boolean ထုတ်ပေးပြီး and/or/not က ပေါင်းစပ်ပါတယ်။ if collection: က ဗလာ မဟုတ်မှ ဝင်ပြီး value or default က value falsy ဖြစ်ရင် default ကို သုံးပါတယ်။

Example: boolean value, comparison, logic နဲ့ truthiness

ဒီ example က True/False ရဲ့ type ကို ပြပြီး comparison သုံးခု၊ logical operator သုံးခုကို စမ်းပါတယ်။ နောက်ဆုံး အပိုင်းမှာ number၊ string နဲ့ list တွေကို bool() နဲ့ စစ်ပြီး ဗလာ value တွေ False ဖြစ်တာကို ပြပါတယ်။ Run မလုပ်ခင် ညာဘက်က comment တွေကို ဖုံးပြီး line တစ်ကြောင်းချင်း ကြိုခန့်မှန်းကြည့်ပါ။ Logical အပိုင်းမှာ learner အများစု မှားလေ့ရှိပါတယ်၊ and က နှစ်ဘက်လုံး လိုပြီး or က တစ်ဘက်ပဲ လိုလို့ပါ။

booleans.py
# ===== 1. Boolean Values =====
x = True
y = False

print(f"x = {x}, Type: {type(x)}")
print(f"y = {y}, Type: {type(y)}")

# ===== 2. Comparison Operations =====
print("\n===== Comparison =====")
print(f"10 > 5 = {10 > 5}")      # True
print(f"10 == 5 = {10 == 5}")    # False
print(f"10 < 5 = {10 < 5}")      # False

# ===== 3. Logical Operators =====
print("\n===== Logical Operators =====")
a = True
b = False

print(f"a and b = {a and b}")    # False
print(f"a or b = {a or b}")      # True
print(f"not a = {not a}")        # False

# ===== 4. Boolean Values of Objects =====
print("\n===== Boolean of Objects =====")
print(f"bool(0) = {bool(0)}")           # False
print(f"bool(1) = {bool(1)}")           # True
print(f"bool('') = {bool('')}")         # False
print(f"bool('Sai') = {bool('Sai')}")   # True
print(f"bool([]) = {bool([])}")         # False
print(f"bool([1,2,3]) = {bool([1,2,3])}")  # True

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 5print(f"x = {x}, Type: {type(x)}")

    True ရဲ့ type က <class 'bool'> ပါ။ bool က int ရဲ့ subclass ဖြစ်လို့ True က 1၊ False က 0 နဲ့ ညီပါတယ်။

  2. Line 11print(f"10 == 5 = {10 == 5}") # False

    == က တန်ဖိုး တူမတူ မေးတာပါ၊ = တစ်ခုတည်းက assign လုပ်တာမို့ နှစ်ခုကို ဘယ်တော့မှ အပြန်အလှန် သုံးလို့ မရပါဘူး။

  3. Lines 19-20print(f"a and b = {a and b}") # False

    a က True ဖြစ်လို့ and က b ကို ဆက်စစ်ပြီး b (False) ကို ပြန်ပေးပါတယ်။ or ကတော့ a က True ဖြစ်တာနဲ့ b ကို မကြည့်တော့ပါဘူး။

  4. Line 27print(f"bool('') = {bool('')}") # False

    ဗလာ string က falsy ဖြစ်ပြီး 'Sai' လို စာလုံး ပါတဲ့ string က truthy ပါ။ စာသားရဲ့ အဓိပ္ပာယ်က အရေးမကြီးပါဘူး။

  5. Line 29print(f"bool([]) = {bool([])}") # False

    ဗလာ list က False ဖြစ်လို့ if cart: လို့ ရေးရုံနဲ့ cart ထဲ ပစ္စည်း ရှိမရှိ စစ်လို့ရပါတယ်၊ len(cart) > 0 မလိုပါဘူး။

You should see
x = True, Type: <class 'bool'>
y = False, Type: <class 'bool'>

===== Comparison =====
10 > 5 = True
10 == 5 = False
10 < 5 = False

===== Logical Operators =====
a and b = False
a or b = True
not a = False

===== Boolean of Objects =====
bool(0) = False
bool(1) = True
bool('') = False
bool('Sai') = True
bool([]) = False
bool([1,2,3]) = True
and က short-circuit လုပ်ပုံ
  1. ဘယ်ဘက် operand ကို တွက်
  2. Falsy ဆိုရင် ချက်ချင်း ပြန်
  3. Truthy ဆိုမှ ညာဘက်ကို တွက်
  4. ညာဘက် value ကို ပြန်

ဘယ်ဘက် falsy

user and user.is_admin -> None (right side skipped)

ဘယ်ဘက် truthy

user and user.is_admin -> value of user.is_admin

any(), all() နဲ့ boolean တွေကို ရေတွက်ခြင်း

Condition တစ်ခုကို value အများကြီးပေါ်မှာ စစ်ချင်ရင် and/or ကို ရှည်ရှည် မဆက်ဘဲ built-in function နှစ်ခုကို သုံးပါတယ်။ all(values) က အားလုံး truthy ဖြစ်မှ True ပြန်ပြီး any(values) က တစ်ခု truthy ဖြစ်တာနဲ့ True ပြန်ပါတယ်။ ဗလာ list အတွက် all([]) က True ဖြစ်ပြီး any([]) က False ဖြစ်တာကို မှတ်ထားပါ — "စစ်စရာ မရှိရင် ဘာမှ မကျရှုံးဘူး" ဆိုတဲ့ logic ပါ။ bool က int ဖြစ်လို့ sum() နဲ့ True ဘယ်နှခု ရှိလဲ ရေတွက်လို့လည်း ရပါတယ်။

any_all.py
passed = [True, True, False, True]

print(all(passed))      # False: one test failed
print(any(passed))      # True: at least one passed
print(sum(passed))      # 3: True counts as 1
print(all([]), any([])) # True False

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Permission စစ်တဲ့ code တိုင်းက boolean logic ပါ — is_logged_in and (is_admin or is_owner) and not is_banned လို condition တွေက web app တိုင်းမှာ ရှိပါတယ်။ Feature flag တွေ (ENABLE_NEW_CHECKOUT = True)၊ form validation ရလဒ်တွေ (is_valid)၊ database ထဲက is_active column တွေကလည်း boolean တွေပါ။ Boolean variable နာမည်ကို is_၊ has_၊ can_ နဲ့ စရင် code ကို စာကြောင်းလို ဖတ်ရလွယ်ပါတယ်။

Short-circuit ကိုလည်း နေ့စဉ် သုံးပါတယ်။ user and user.email က user မရှိ (None) ရင် user.email ကို မဖတ်တော့လို့ AttributeError ကို ကာကွယ်ပေးပါတယ်။ name = form.get("name") or "Anonymous" လို default value ပေးတာကလည်း အသုံးများပါတယ်။ ဒါပေမယ့် 0 က valid value ဖြစ်နိုင်တဲ့ နေရာ (quantity၊ discount) မှာ or သုံးရင် 0 ကို default နဲ့ အစားထိုးသွားလို့ is None နဲ့ သီးသန့် စစ်ရပါတယ်။ Test တွေကလည်း boolean ကို အားကိုးပါတယ် — assert statement တိုင်းက expression တစ်ခု truthy ဖြစ်မဖြစ်ကိုပဲ စစ်တာပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. x == a or b လို့ ရေးတာ

role == "admin" or "editor" က "editor" string က truthy ဖြစ်လို့ အမြဲ ဝင်ပါတယ်။ role in ("admin", "editor") လို့ ရေးပါ၊ ပိုတိုပြီး မှားဖို့ မလွယ်ပါဘူး။

2. true / "True" လို့ ရေးတာ

is_ready = true က NameError: name 'true' is not defined ဖြစ်ပါတယ် (JavaScript နဲ့ မတူပါ)။ "True" လို quote နဲ့ ရေးရင် string ဖြစ်ပြီး "False" string က if ထဲမှာ truthy ဖြစ်နေပါမယ်။

3. if x == True: လို့ ရေးတာ

if x == True: က x က 1 ဆိုရင် ဝင်ပေမယ့် x က "yes" ဒါမှမဟုတ် [1] ဆိုရင် မဝင်ပါဘူး။ Boolean variable အတွက် if is_ready: လို့ပဲ ရေးပြီး "လုံးဝ False ဖြစ်မှ" လိုရင် if x is False: ကို သုံးပါ။

4. not ရဲ့ priority ကို မေ့တာ

not a == b က not (a == b) ဖြစ်ပြီး (not a) == b မဟုတ်ပါဘူး၊ not က comparison တွေထက် နောက်မှ အလုပ်လုပ်လို့ပါ။ ဒါပေမယ့် not a and b ကတော့ (not a) and b ပါ။ ဒီလို အသေးစိတ်ကို ကျက်မှတ်နေမယ့်အစား a != b လို ရိုးရှင်းတဲ့ ပုံစံ ဒါမှမဟုတ် ကွင်းကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Boolean variable တွေကို is_active၊ has_items၊ can_edit လို မေးခွန်းပုံစံ နာမည်ပေးပါ။
  • Condition ရှည်ရင် အစိတ်အပိုင်းတွေကို နာမည်ပါ variable တွေထဲ ခွဲထည့်ပါ။
  • None ကို စစ်ရင် == None မဟုတ်ဘဲ is None ကို သုံးပါ။

Mini challenge

Mini challenge: edit ခွင့် စစ်တဲ့ function

Blog system အတွက် ဘယ်သူ edit လုပ်ခွင့် ရှိလဲ ဆုံးဖြတ်တဲ့ function နဲ့ ဗလာ nickname အတွက် "Guest" ပြန်ပေးတဲ့ function ရေးပါ။

  • can_edit(role, is_banned, is_owner) — admin, editor ဒါမှမဟုတ် owner ဖြစ်ရမယ်၊ banned ဆိုရင် ဘယ်တော့မှ မရ
  • and, or, not နဲ့ ကွင်းကို သုံးပြီး return statement တစ်ကြောင်းတည်းနဲ့ ရေးရမယ်
  • display_name(nickname) — space တွေပဲ ပါရင်တောင် "Guest" ပြန်ပေးရမယ်
  • Bug ရှိတဲ့ 'viewer' == 'admin' or 'editor' ရဲ့ ရလဒ်ကိုလည်း print ထုတ်ပြရမယ်
can_edit.py
def can_edit(role: str, is_banned: bool, is_owner: bool) -> bool:
    # TODO: (admin or editor, or the owner) and not banned
    ...


def display_name(nickname: str) -> str:
    # TODO: strip the nickname and fall back to "Guest" with or
    ...


checks = [
    ("editor", False, False),
    ("viewer", False, True),
    ("admin", True, False),
    ("viewer", False, False),
]
# TODO: loop over checks and print each case with its can_edit result
# TODO: print display_name("  ") and display_name("Mya")
# TODO: print bool('viewer' == 'admin' or 'editor')

ရလဒ်: Program က အောက်ပါအတိုင်း ထုတ်ပေးရပါမယ်: editor, banned=False, owner=False -> True viewer, banned=False, owner=True -> True admin, banned=True, owner=False -> False viewer, banned=False, owner=False -> False display_name(' ') = Guest display_name('Mya') = Mya Buggy check gives: True

အရိပ်အမြွက် ကြည့်ရန်

role in ("admin", "editor") က role နှစ်မျိုးကို တစ်ခါတည်း စစ်ပါတယ်။ not က and ထက် အရင် အလုပ်လုပ်ပေမယ့် or ကိုတော့ ကွင်းခတ်ရပါမယ်၊ မခတ်ရင် banned admin က edit လုပ်နိုင်သွားပါမယ်။ for role, banned, owner in checks: က tuple တစ်ခုချင်းကို variable သုံးခုထဲ ခွဲထည့်ပေးပါတယ်။

အဖြေ ကြည့်ရန်
can_edit.py
def can_edit(role: str, is_banned: bool, is_owner: bool) -> bool:
    """Admins and editors can edit, owners can edit their own post,
    and banned users can never edit."""
    return (role in ("admin", "editor") or is_owner) and not is_banned


def display_name(nickname: str) -> str:
    """Fall back to 'Guest' when the nickname is empty or only spaces."""
    return nickname.strip() or "Guest"


checks = [
    ("editor", False, False),
    ("viewer", False, True),
    ("admin", True, False),
    ("viewer", False, False),
]
for role, banned, owner in checks:
    print(f"{role}, banned={banned}, owner={owner} -> {can_edit(role, banned, owner)}")

print(f"display_name('  ') = {display_name('  ')}")
print(f"display_name('Mya') = {display_name('Mya')}")
print(f"Buggy check gives: {bool('viewer' == 'admin' or 'editor')}")

Quick Quiz 1 / 3

print(0 or "zero", 5 and 7) က ဘာ ထုတ်မလဲ။

Quick Quiz 2 / 3

ဒီ value တွေထဲက ဘယ်ဟာက truthy လဲ။

Quick Quiz 3 / 3

role = "guest" ဖြစ်ရင် ဘယ် condition က False ပြန်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Boolean value နှစ်ခုက True နဲ့ False ဖြစ်ပြီး comparison တွေက ထုတ်ပေးပါတယ်။
  • and/or က short-circuit လုပ်ပြီး operand တစ်ခုကို ပြန်ပေးပါတယ်၊ priority က not, and, or ပါ။
  • False, None, 0, 0.0 နဲ့ ဗလာ collection တွေက falsy ဖြစ်ပြီး ကျန်တာ အားလုံး truthy ပါ။
  • x == a or b မဟုတ်ဘဲ x in (a, b) လို့ ရေးပါ။

The Python Tutorial — Python Software Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Python Booleans | Thuta Learning