Thuta Learning
Python
ExercisesProgrammingအခြေခံ

Exercise Set 1: Basics Practice (Variables, Strings, Lists, Loops)

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

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

  • Code မရေးခင် task တစ်ခုစီအတွက် normal၊ edge နဲ့ invalid input example တွေ ရေးနိုင်ဖို့
  • print မလုပ်ဘဲ value ကို return ပြန်တဲ့ function သေးလေးတွေ ရေးနိုင်ဖို့
  • input() က str ပြန်ပေးတာကို နားလည်ပြီး int() နဲ့ program ရဲ့ အစွန်းမှာ ပြောင်းနိုင်ဖို့
  • assert နဲ့ edge case တွေကို စစ်ပြီး fail ဖြစ်ရင် traceback ကို ဖတ်နိုင်ဖို့

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

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

ဒီ lesson မှာ အသစ်တစ်ခုမှ မသင်ပါဘူး။ Basics chapter မှာ သင်ခဲ့တဲ့ variable၊ data type၊ string၊ list၊ loop နဲ့ function တွေကို ကိုယ်တိုင် စစ်ဆေးဖို့ပါ။ ဂီတသမားတွေ scale တွေကို နေ့တိုင်း လေ့ကျင့်သလိုပဲ သိပြီးသား အရာတွေကို ကိုယ့်လက်နဲ့ ထပ်ခါထပ်ခါ ရေးမှ စွဲမြဲသွားပါတယ်။

Task တစ်ခုစီကို ကိုယ်တိုင် ရေးပြီး run ကြည့်ပါ။ ပိတ်မိရင် အရင် lesson တွေကို ပြန်ကြည့်တာ ကောင်းပါတယ်။ ရည်ရွယ်ချက်က အဖြေမှန်ကို copy ကူးဖို့ မဟုတ်ဘဲ logic ကို ကိုယ်တိုင် တွေးတောနိုင်ဖို့ပါ။ ဒီ lesson မှာ task တွေအပြင် task တိုင်းကို ချဉ်းကပ်တဲ့ နည်းလမ်းတစ်ခုကိုပါ လေ့ကျင့်ပါမယ်။

Problem → Why → Solution

ပြဿနာ: တစ်ကြိမ် မှန်ရုံနဲ့ မလုံလောက်ဘူး

Learner တစ်ယောက်ရဲ့ ပထမ draft ကို ကြည့်ပါ။ count_vowels("Python Programming") က 4 လို့ print ထုတ်ပြီး အဖြေမှန်လို့ နောက် task ကို ဆက်သွားပါတယ်။ ဒါပေမယ့် "AEIOU" ကို စမ်းကြည့်ရင် 0 ရပါတယ်၊ စာလုံးအကြီးတွေကို မေ့ထားလို့ပါ။ Function က return မလုပ်ဘဲ print လုပ်ထားလို့ တခြား code က result ကို သုံးလို့လည်း စစ်လို့လည်း မရပါဘူး။

Age အပိုင်းမှာ ပိုဆိုးပါတယ်။ input() က user ရိုက်တာကို str အဖြစ်ပဲ ပြန်ပေးလို့ age + 1 က TypeError နဲ့ ရပ်သွားပါတယ်။ Sample တစ်ခုတည်းနဲ့ စမ်းခဲ့ရင် ဒီ bug နှစ်ခုလုံးကို မတွေ့နိုင်ပါဘူး။

draft.py
def count_vowels(text):
    count = 0
    for ch in text:
        if ch in "aeiou":          # capital letters are never counted
            count += 1
    print(count)                   # prints, so nothing can test the result

count_vowels("Python Programming")  # 4 - looks finished
age = input("Age: ")
print(age + 1)                     # TypeError: input() returned a str

ဘာကြောင့် ဒီ bug တွေ လွတ်သွားတာလဲ

Task ထဲက sample ဟာ ဖြစ်နိုင်တဲ့ input တွေထဲက တစ်ခုပဲ ဖြစ်ပါတယ်။ စာလုံးအကြီး၊ string အလွတ်၊ list အလွတ်၊ number အနှုတ် စတဲ့ အစွန်းတွေကို ကြိုမစဉ်းစားခဲ့ရင် program က မှန်တယ်လို့ ယုံကြည်ဖို့ အကြောင်း မရှိပါဘူး။ Value ကို return မပြန်ရင် အဲဒီ case တွေကို အလိုအလျောက် စစ်ဖို့လည်း မဖြစ်နိုင်ပါဘူး။

Core concept: exercise တိုင်းအတွက် အဆင့်ငါးဆင့်

Exercise တွေက syntax မှတ်မိလား စစ်တာ မဟုတ်ပါဘူး။ Problem တစ်ခုကို input၊ စစ်ဆေးမှု၊ ပြောင်းလဲမှု နဲ့ output အဖြစ် ခွဲနိုင်လား စစ်တာပါ။ ဒီ task လေးခုလုံးမှာ အောက်က အဆင့်တွေကို တစ်ခုချင်း လိုက်ကြည့်ပါ။

၁။ Example တွေကို အရင်ရေးပါ

Code မရေးခင် input → မျှော်လင့်ထားတဲ့ output ကို စာရင်းလုပ်ပါ။ Task ထဲက normal case တစ်ခု၊ string အလွတ် ဒါမှမဟုတ် list အလွတ်လို edge case တစ်ခု၊ "abc" ကို age အဖြစ် ပေးတာလို invalid case တစ်ခု အနည်းဆုံး ပါပါစေ။ ဒီ table က သင့်ရဲ့ test တွေ ဖြစ်လာပါမယ်။

၂။ အဆင့်တွေကို comment နဲ့ စီစဉ်ပါ

"စာလုံးတစ်လုံးချင်း loop ပတ်၊ vowel ဆိုရင် count တိုး၊ count ကို return ပြန်" လို pseudocode သုံးကြောင်းက code ရဲ့ ပုံစံကို ဆုံးဖြတ်ပေးပါတယ်။

၃။ Value ကို return ပြန်ပါ

Task တစ်ခုစီကို value return ပြန်တဲ့ function တစ်ခုအဖြစ် ရေးပါ။ input() နဲ့ print() ကို program ရဲ့ အစွန်းမှာပဲ ထားပါ။ ဒါမှ function ကို စစ်လို့ရပြီး တခြားနေရာမှာ ပြန်သုံးလို့ရပါတယ်။

၄။ assert နဲ့ စစ်ပါ

assert count_vowels("") == 0 လို line တစ်ကြောင်းက မှန်ရင် ဘာမှမဖြစ်ဘဲ မှားရင် AssertionError နဲ့ line နံပါတ်ကို ပြပါတယ်။ Fail ဖြစ်ရင် traceback ကို အောက်ဆုံးကနေ ဖတ်ပြီး fail ဖြစ်စေတဲ့ အသေးဆုံး input ကို ရှာပါ။

၅။ မှန်ပြီးမှ သပ်ရပ်အောင် ပြင်ပါ

Assert အားလုံး pass ဖြစ်ပြီးမှ loop ကို list comprehension နဲ့ အစားထိုးတာလို ပြင်ဆင်ပါ။ Assert တွေက ပြင်ဆင်မှုက ဘာမှ မဖျက်ဆီးဘူးဆိုတာ ချက်ချင်း ပြောပြပါတယ်။

TaskNormal → outputEdge → output
count_vowels"Python Programming" → 4"rhythm" → 0
evens[4, 7, 2] → [4, 2][-4, -3, 0] → [-4, 0]
multiples_of_350 → 3 ... 482 → []
Mental model: example → code → စစ်ဆေး
  1. Code မရေးခင် example တွေ ရေး
  2. အဆင့်တွေကို comment နဲ့ စီစဉ်
  3. Function ကနေ value ကို return
  4. Normal၊ edge၊ invalid ကို assert
  5. Pass ဖြစ်မှ refactor

Example table ကို ရေးတာက အချိန်ကုန်တယ်လို့ ထင်ရပေမယ့် debug လုပ်ရမယ့် အချိန်ကို အများကြီး သက်သာစေပါတယ်။

အခြေခံ ပုံစံ

python
def solve(data):
    result = ...             # build the answer step by step
    return result            # return it; let the caller decide to print

assert solve(normal_input) == expected_output
assert solve(edge_input) == edge_output
print(solve(normal_input))

Function က အဖြေကို return ပြန်ပြီး assert တွေက example table ကို code အဖြစ် ပြောင်းထားတာပါ။ print() ကို အဆုံးမှာ တစ်ကြိမ်ပဲ ခေါ်ပါတယ်။

Task လေးခုရဲ့ အဖြေ (input() မပါဘဲ)

Task 1 မှာ input() ကို တိုက်ရိုက် မခေါ်ပါဘူး။ ဒီလို ခေါ်ထားတဲ့ script က user မရှိတဲ့ နေရာ (test၊ automated runner) မှာ ရပ်နေမှာပါ။ အဲဒီအစား greeting ကို greet(name, age) function အဖြစ် ရေးပြီး value တွေကို argument အဖြစ် ပေးပါတယ်။ Task 4 ကလည်း number ၁၆ ခုကို line ၁၆ ကြောင်း မထုတ်ဘဲ line တစ်ကြောင်းတည်းမှာ ပြပါတယ်။ နောက်ဆုံးမှာ edge case assert တွေ ပါပါတယ်။

basics_practice.py
# Task 1: Greeting - build the sentence in a function so it can be tested.
# In an interactive script the values would come from input(), which always
# returns a str: name = input("Name: "); age = int(input("Age: "))
def greet(name, age):
    return f"Hi {name}, you are {age} years old"


# Task 2: Count vowels (upper and lower case)
def count_vowels(text):
    count = 0
    for ch in text.lower():
        if ch in "aeiou":
            count += 1
    return count


# Task 3: Keep only the even numbers
def evens(numbers):
    return [n for n in numbers if n % 2 == 0]


# Task 4 (bonus): multiples of 3 from 1 to 50
def multiples_of_3(limit):
    return [i for i in range(1, limit + 1) if i % 3 == 0]


print(greet("Aye Aye", 21))
print("Vowels in 'Python Programming':", count_vowels("Python Programming"))
print("Even numbers:", evens([4, 7, 2, 9, 10, 15, 6]))
print("Multiples of 3:", *multiples_of_3(50))

# Edge cases: one sample output is not proof, so check the corners too
assert count_vowels("") == 0
assert count_vowels("rhythm") == 0          # y is not in our vowel list
assert count_vowels("AEIOU") == 5           # upper case counts
assert evens([]) == []
assert evens([-4, -3, 0]) == [-4, 0]        # negatives and zero are fine
assert multiples_of_3(2) == []
print("All edge-case checks passed")

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

  1. Return the sentencedef greet(name, age):

    Function က string ကို return ပြန်လို့ assert greet("Su", 9) == "Hi Su, you are 9 years old" လို စစ်လို့ရပါတယ်။ input() ကတော့ program ရဲ့ အစွန်း (main code) မှာပဲ ရှိသင့်ပါတယ်။

  2. Lower-case oncefor ch in text.lower():

    Text တစ်ခုလုံးကို lower() နဲ့ ပြောင်းလိုက်လို့ "aeiou" ငါးလုံးနဲ့ပဲ နှိုင်းယှဉ်ရုံနဲ့ စာလုံးအကြီး/အသေး နှစ်မျိုးလုံး ရေတွက်ပါတယ်။

  3. Filter with a comprehensionreturn [n for n in numbers if n % 2 == 0]

    n % 2 == 0 က number အနှုတ်နဲ့ 0 အတွက်လည်း မှန်ပါတယ် (-4 % 2 က 0)။ Comprehension က list အသစ်တစ်ခု ဖန်တီးလို့ မူလ list မပြောင်းပါဘူး။

  4. range() stops before its endreturn [i for i in range(1, limit + 1) if i % 3 == 0]

    range(1, limit) က limit ကို မပါဝင်လို့ "50 အထိ" ဆိုရင် limit + 1 ရေးရပါတယ်။ ဒါမှ limit ကိုယ်တိုင် 3 ရဲ့ ဆတိုး (ဥပမာ 51) ဖြစ်ရင် ပါဝင်ပါတယ်။

  5. Unpack into printprint("Multiples of 3:", *multiples_of_3(50))

    * က list ကို argument တစ်ခုချင်းစီအဖြစ် ဖြန့်ပေးလို့ print() က space ခြားပြီး line တစ်ကြောင်းတည်းမှာ ထုတ်ပါတယ်။

  6. Edge cases as assertsassert count_vowels("rhythm") == 0 # y is not in our vowel list

    Task ရဲ့ sample က ဘယ်တော့မှ မစမ်းတဲ့ case တွေကို assert က စစ်ပါတယ်။ တစ်ခုခု မှားရင် "All edge-case checks passed" မထွက်ဘဲ AssertionError ရပါတယ်။

You should see
Hi Aye Aye, you are 21 years old
Vowels in 'Python Programming': 4
Even numbers: [4, 2, 10, 6]
Multiples of 3: 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48
All edge-case checks passed

input() က str ပဲ ပြန်ပေးတယ်

User က 21 လို့ ရိုက်ရင်တောင် input() က "21" ဆိုတဲ့ string ကို ပြန်ပေးပါတယ်။ String နဲ့ "1" ကို ပေါင်းရင် စာသား ဆက်သွားလို့ "211" ရပြီး number 1 နဲ့ ပေါင်းရင် TypeError ရပါတယ်။ ဒါကြောင့် int() နဲ့ program ရဲ့ အစွန်းမှာ တစ်ကြိမ် ပြောင်းပြီး ကျန်တဲ့ code ကို number နဲ့ပဲ အလုပ်လုပ်စေပါ။ User က "abc" ရိုက်ရင် int() က ValueError raise လုပ်တာကိုလည်း သတိထားပါ။ Challenge မှာ ဒါကို ကိုင်တွယ်ရပါမယ်။

input_types.py
raw = "21"                      # what input() hands you when the user types 21
print(type(raw).__name__, raw + "1")

age = int(raw)                  # convert once, at the edge of the program
print(type(age).__name__, age + 1)

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

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

ဒီ task တွေက သေးပေမယ့် အလေ့အကျင့်က အလုပ်ခွင်မှာ တိုက်ရိုက် သုံးရပါတယ်။ Coding interview တွေမှာ interviewer တွေက code မရေးခင် example နဲ့ edge case တွေကို မေးလေ့ရှိပြီး empty input ကို မေ့တာက အဖြစ်အများဆုံး အမှားတစ်ခုပါ။ Team တွေက ဒီ lesson ရဲ့ assert တွေကို pytest test function တွေအဖြစ် ရေးပါတယ်။ ဥပမာ def test_count_vowels_empty(): assert count_vowels("") == 0 လိုပါ။ Code review မှာ reviewer က "string အလွတ်ဆိုရင် ဘာဖြစ်မလဲ" လို့ မေးတာကလည်း ဒီ step 1 ကိုပဲ မေးတာပါ။

ဒီ exercise ကို ပြီးအောင်လုပ်တဲ့အခါ task တစ်ခုစီအတွက် example table၊ code နဲ့ assert ပါတဲ့ ရလဒ်တစ်ခုကို ကိုယ့်ကိုယ်ကို ပြပါ။ ဒါက အလုပ်ခွင်မှာ pull request တစ်ခု တင်တာနဲ့ ပုံစံတူပါတယ်။ Pull request မှာလည်း ဘာပြောင်းလဲ၊ ဘယ် case တွေကို စစ်ခဲ့လဲ၊ test တွေ pass ဖြစ်လား ဆိုတာကို reviewer က တစ်ချက်တည်းနဲ့ မြင်ချင်ပါတယ်။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

Return အစား print လုပ်တာ

print() ပဲ လုပ်တဲ့ function က None ပြန်ပါတယ်။ ဒါကြောင့် total = count_vowels(text) + 1 က TypeError ဖြစ်ပြီး assert နဲ့လည်း စစ်လို့ မရပါဘူး။ Return ပြန်ပြီး ခေါ်တဲ့သူက print လုပ်ပါစေ။

input() ရဲ့ str ကို number လို သုံးတာ

age = input() ပြီးနောက် age + 1 က TypeError ဖြစ်ပြီး age * 2 ကတော့ error မပေါ်ဘဲ "2121" ကို ပြန်ပေးပါတယ်။ ဒုတိယတစ်ခုက ပိုအန္တရာယ်များပါတယ်၊ ဘာ error မှ မပြလို့ပါ။

range() ရဲ့ အဆုံးကို မေ့တာ

range(1, 50) က 49 မှာ ရပ်ပါတယ်။ "1 ကနေ 50 အထိ" ဆိုရင် range(1, 51) ရေးရပါတယ်။ Off-by-one error တွေက sample output မှာ မပေါ်ဘဲ အစွန်းဆုံး value မှာပဲ ပေါ်တတ်ပါတယ်။

User input ကို assert နဲ့ စစ်တာ

assert က developer အတွက် စစ်ဆေးမှုပါ။ python -O နဲ့ run ရင် assert တွေ အကုန် ကျော်သွားလို့ user input ကို စစ်ဖို့ if နဲ့ ValueError ကို သုံးပါ။

Best Practices

Best practices

  • Task တစ်ခုကို ၁၅ မိနစ်ထက် ပိုပိတ်နေရင် example table ကို ပြန်ကြည့်ပါ၊ ပြဿနာက syntax ထက် နားလည်မှုမှာ ရှိလေ့ရှိပါတယ်
  • Function နာမည်ကို ဘာ return ပြန်လဲ ပြောပြတဲ့ နာမည် (count_vowels၊ evens) ပေးပါ
  • Bug တစ်ခု တွေ့တိုင်း အဲဒါကို ဖမ်းမယ့် assert တစ်ကြောင်း ထည့်ပြီးမှ ပြင်ပါ
  • Solution ကို ကြည့်ပြီးရင် နောက်နေ့မှာ မကြည့်ဘဲ ထပ်ရေးကြည့်ပါ

Mini challenge

Basics task အသစ် လေးခု

အောက်က function လေးခုကို ရေးပါ။ တစ်ခုစီအတွက် code မရေးခင် normal နဲ့ edge example တွေကို ကြိုရေးပြီး assert အနည်းဆုံး တစ်ခုစီ ထည့်ပါ။

  • initials("aung kyaw moe") က "A.K.M." ပြန်ပြီး string အလွတ်ဆိုရင် "" ပြန်ပါ
  • long_words(text, min_length) က punctuation ဖယ်ပြီး min_length နဲ့အထက် ရှည်တဲ့ word တွေကို ပြန်ပေးပါ
  • running_total([5, 10, -3, 8]) က [5, 15, 12, 20] ပြန်ပါ
  • parse_age(text) က 1 ကနေ 129 ကြားဆိုရင် int၊ မဟုတ်ရင် None ပြန်ပါ ("abc"၊ "-4"၊ "" အပါအဝင်)
basics_challenge.py
def initials(full_name):
    # TODO: split into words; join the first letter of each, upper-cased, with "."
    ...


def long_words(text, min_length):
    # TODO: split, strip ".,!?" from each word, keep the long ones
    ...


def running_total(numbers):
    # TODO: keep a running sum and append it after each number
    ...


def parse_age(text):
    # TODO: int() inside try/except ValueError; None if invalid or out of range
    ...


# TODO: print one call per function, then assert at least one edge case each

ရလဒ်: A.K.M.၊ ['Python', 'loops', 'powerful']၊ [5, 15, 12, 20] နဲ့ [21, 35, None, None, None, None] ထွက်ပြီး နောက်ဆုံးမှာ All checks passed ပေါ်ပါမယ်။

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

" su su ".split() က space ပိုနေတာတွေကို အလိုလို ဖယ်ပေးလို့ ["su", "su"] ရပါတယ်။ int(" 35 ") က space ကို လက်ခံပြီး int("abc") က ValueError raise လုပ်ပါတယ်။

အဖြေ ကြည့်ရန်
basics_challenge.py
def initials(full_name):
    parts = full_name.split()
    return ".".join(p[0].upper() for p in parts) + "." if parts else ""


def long_words(text, min_length):
    words = [w.strip(".,!?") for w in text.split()]
    return [w for w in words if len(w) >= min_length]


def running_total(numbers):
    totals, total = [], 0
    for n in numbers:
        total += n
        totals.append(total)
    return totals


def parse_age(text):
    try:
        age = int(text)  # int() ignores surrounding spaces
    except ValueError:
        return None
    return age if 0 < age < 130 else None


print(initials("aung kyaw moe"))
print(long_words("Python is fun, and loops are powerful!", 5))
print(running_total([5, 10, -3, 8]))
print([parse_age(t) for t in ["21", " 35 ", "abc", "-4", "200", ""]])

assert initials("") == ""
assert initials("  su   su ") == "S.S."
assert long_words("", 3) == []
assert running_total([]) == []
print("All checks passed")

Quick Quiz 1 / 3

Example ရဲ့ count_vowels() နဲ့ print(count_vowels("Rhythm & Blues")) က ဘာထုတ်မလဲ။

Quick Quiz 2 / 3

age = input("Age: ") မှာ user က 21 ရိုက်ပါတယ်။ print(age + 1) က ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

1 ကနေ 50 အထိ (50 ပါဝင်) 3 နဲ့ စားလို့ပြတ်တဲ့ number တွေကို ဘယ် range() က တိတိကျကျ ထုတ်ပေးလဲ။

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

  • Code မရေးခင် normal၊ edge၊ invalid example တွေကို ရေးပါ
  • Task တစ်ခုစီကို value return ပြန်တဲ့ function အဖြစ် ရေးပြီး input()/print() ကို အစွန်းမှာ ထားပါ
  • input() က str ပြန်လို့ int() နဲ့ တစ်ကြိမ် ပြောင်းပြီး ValueError ကို ကိုင်တွယ်ပါ
  • assert က developer check ဖြစ်ပြီး user input ကိုတော့ if နဲ့ စစ်ပါ

The Python Tutorial — Python Software Foundation

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

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

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

Exercise Set 1: Basics Practice (Variables, Strings, Lists, Loops) | Thuta Learning