Thuta Learning
Python
AdvancedProgrammingအခြေခံ

Built-in Functions (Enhanced)

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

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

  • Built-in တွေကို inspect၊ convert၊ aggregate၊ order၊ iterate ဆိုတဲ့ အုပ်စုငါးစုနဲ့ ခွဲမှတ်နိုင်ဖို့
  • max()၊ min()၊ sorted() မှာ key= နဲ့ default= ကို သုံးပြီး empty input ကို ကိုင်တွယ်နိုင်ဖို့
  • map()၊ filter()၊ zip()၊ enumerate() က lazy iterator ပြန်ပေးတယ်ဆိုတာ ရှင်းပြနိုင်ဖို့
  • list၊ sum၊ str လို built-in နာမည်တွေကို variable နာမည်အဖြစ် မသုံးမိအောင် ရှောင်နိုင်ဖို့

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

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

Built-in function တွေဆိုတာ Python ကို install လုပ်လိုက်တာနဲ့ ပါလာတဲ့ ကိရိယာသေတ္တာထဲက tool တွေပါ။ len() က အရေအတွက်ကို ရေတွက်ပေးတယ်၊ sum() က ပေါင်းပေးတယ်၊ sorted() က စီပေးတယ်။ import တစ်ကြောင်းမှ မရေးဘဲ ဘယ် file ထဲမှာမဆို တန်းသုံးလို့ ရပါတယ်။

ဒီ tool တွေက C နဲ့ ရေးထားလို့ ကိုယ်တိုင်ရေးတဲ့ for loop ထက် များသောအားဖြင့် မြန်ပြီး bug နည်းပါတယ်။ ဒါပေမဲ့ tool တစ်ခုစီမှာ သူ့စည်းကမ်းလေးတွေ ရှိပါတယ်။ ဗလာ list ပေးရင် ဘာဖြစ်မလဲ၊ ရလဒ်ကို တစ်ခါပဲ သုံးလို့ရလား၊ ဘယ်လို စီမလဲဆိုတာကို သိထားမှ မှန်မှန်ကန်ကန် သုံးနိုင်မှာပါ။

Problem → Why → Solution

ပြဿနာ: built-in နာမည်ကို ဖုံးပစ်မိတာ

Shopping cart script တစ်ခုမှာ ဈေးနှုန်းတွေကို list လို့ နာမည်ပေးပြီး စုစုပေါင်းကို sum လို့ နာမည်ပေးလိုက်ပါတယ်။ Script ရဲ့ အပေါ်ပိုင်းမှာ ဘာမှ မမှားသလို ထင်ရပါတယ်။

နောက်ပိုင်း range() ကို list အဖြစ် ပြောင်းဖို့ list(range(3)) ကို ခေါ်လိုက်တဲ့အခါ TypeError: 'list' object is not callable တက်လာပါတယ်။ sum() ကို ခေါ်တဲ့နေရာမှာလည်း 'int' object is not callable ဖြစ်ပါတယ်။ Error တက်တဲ့ line နဲ့ အမှန်တကယ် အမှားလုပ်ခဲ့တဲ့ line က အဝေးကြီး ကွာနေတတ်လို့ ရှာရ ခက်ပါတယ်။

cart.py
list = [4.5, 12.0, 3.25]      # prices
sum = 0
for price in list:
    sum += price

# ... many lines later ...
ids = list(range(3))     # TypeError: 'list' object is not callable
total = sum([1, 2])      # TypeError: 'int' object is not callable

ဘာကြောင့် ဒီလိုဖြစ်တာလဲ

Python က နာမည်တစ်ခုကို Local၊ Enclosing၊ Global၊ Built-in (LEGB) အစဉ်အတိုင်း ရှာပါတယ်။ Built-in က နောက်ဆုံးအဆင့်ပါ။ ဒါကြောင့် global မှာ list ဆိုတဲ့ variable ရှိသွားတာနဲ့ Python က အဲဒါကို အရင်တွေ့ပြီး built-in list ဆီ မရောက်တော့ပါဘူး။ Built-in ကို ဖျက်လိုက်တာ မဟုတ်ဘဲ ဖုံးလိုက်တာပါ။ del list လုပ်ရင် ပြန်မြင်ရပါတယ်။

Core concept: built-in ငါးအုပ်စုနဲ့ သူတို့ရဲ့ စည်းကမ်းများ

Built-in function ဆယ်ဂဏန်းကျော်ကို တစ်ခုချင်း အလွတ်ကျက်တာထက် သူတို့လုပ်တဲ့ အလုပ်အလိုက် အုပ်စုခွဲပြီး မှတ်တာ ပိုလွယ်ပါတယ်။ အုပ်စုတစ်ခုချင်းမှာ မေးရမယ့် မေးခွန်းတွေက တူတူပါပဲ: ဗလာ input ဆိုရင် ဘာပြန်ပေးမလဲ၊ ရလဒ်က list အပြည့်လား iterator လား၊ နှိုင်းယှဉ်ပုံကို key= နဲ့ ပြောင်းလို့ရလား။

အုပ်စုBuilt-in များသတိထားရမယ့်အချက်
Inspectlen, type, isinstanceType စစ်ရင် type() == ထက် isinstance() ကို သုံးပါ
Convertint, float, str, listint('3.5') က ValueError တက်ပါတယ်
Aggregatesum, min, max, any, allmax([]) က ValueError၊ sum([]) က 0
Ordersorted, reversedsorted() က list အသစ်ပြန်ပေးပြီး stable ဖြစ်ပါတယ်
Iteraterange, enumerate, zip, map, filter, iter, nextLazy ဖြစ်ပြီး တစ်ခါပဲ ဖြတ်သွားလို့ရပါတယ်

၁။ Empty input ကို ကြိုဆုံးဖြတ်ပါ

sum([]) က 0၊ any([]) က False၊ all([]) က True ပြန်ပေးပါတယ်။ all([]) က True ဖြစ်တာ အံ့သြစရာ ဖြစ်နိုင်ပေမဲ့ "မအောင်တဲ့လူ တစ်ယောက်မှ မရှိဘူး" လို့ ဖတ်ရင် အဓိပ္ပာယ်ရှိပါတယ်။ max() နဲ့ min() ကတော့ ဗလာ input မှာ ValueError တက်ပါတယ်။ default= ပေးထားရင် error အစား အဲဒီ value ကို ပြန်ပေးပါတယ်။

၂။ key= နဲ့ နှိုင်းယှဉ်ပုံကို ပြောင်းပါ

key= က item တစ်ခုချင်းကို function တစ်ခုနဲ့ ပြောင်းပြီး ရလာတဲ့ value နဲ့ နှိုင်းယှဉ်ပါတယ်။ ဒါပေမဲ့ ပြန်ပေးတာက မူရင်း item ပါ။ max(words, key=len) က အရှည်ဆုံး စကားလုံးကို ပြန်ပေးတာ၊ အရှည် number ကို ပြန်ပေးတာ မဟုတ်ပါဘူး။ sorted() က stable ဖြစ်လို့ key တူတဲ့ item တွေရဲ့ မူရင်းအစဉ်ကို ထိန်းထားပါတယ်။

၃။ Lazy ရလဒ်ကို တစ်ခါပဲ သုံးလို့ရတာ မှတ်ထားပါ

map()၊ filter()၊ zip()၊ enumerate()၊ reversed() တို့က list မဟုတ်ဘဲ iterator ကို ပြန်ပေးပါတယ်။ Item တွေကို လိုတဲ့အချိန်ကျမှ တစ်ခုချင်း ထုတ်ပေးလို့ memory သက်သာပါတယ်။ ဒါပေမဲ့ တစ်ခါ loop ပတ်ပြီးရင် ကုန်သွားပါပြီ။ ဒုတိယအကြိမ် ပတ်ရင် ဘာမှ မထွက်တော့ပါဘူး။ ပြန်သုံးဖို့လိုရင် list() နဲ့ အရင် သိမ်းထားပါ။

၄။ zip() ရဲ့ အတိုဆုံးမှာ ရပ်တဲ့ အပြုအမူ

zip(names, ages) မှာ list တစ်ခုက တိုနေရင် ပိုနေတဲ့ item တွေကို ဘာမှမပြောဘဲ ချန်ခဲ့ပါတယ်။ Python 3.10 ကစပြီး zip(names, ages, strict=True) လို့ ရေးရင် အရှည်မတူတဲ့အခါ ValueError တက်လို့ data ပျောက်တာကို ချက်ချင်း သိနိုင်ပါတယ်။

Mental model: built-in မသုံးခင် မေးခွန်းလေးခု
  1. ဘယ်အုပ်စုလဲ (inspect/convert/...)
  2. Empty input ဆိုရင် ဘာဖြစ်မလဲ
  3. List လား iterator လား
  4. key= ဒါမှမဟုတ် default= လိုလား

ဒီမေးခွန်းလေးခုကို ဖြေနိုင်ရင် built-in တစ်ခုက ထုတ်ပေးမယ့် ရလဒ်ကို run မလုပ်ခင် ကြိုပြောနိုင်ပါပြီ။

အခြေခံ ပုံစံ

python
size = len(collection)
best = max(items, key=score_of, default=fallback)
ordered = sorted(items, key=score_of, reverse=True)
total = sum(values, start)
ok = all(condition(x) for x in items)
for position, item in enumerate(items, start=1):
    ...
for left, right in zip(first, second, strict=True):
    ...

Aggregate built-in တွေမှာ key= နဲ့ default= ကို လိုသလို ထည့်ပါ။ Iterate built-in တွေကတော့ for loop ထဲမှာ တိုက်ရိုက် unpack လုပ်ပြီး သုံးလေ့ရှိပါတယ်။

ဥပမာ: built-in တွေကို တစ်ခုချင်း စမ်းကြည့်မယ်

ဒီ script က built-in တွေကို အုပ်စုလိုက် ခေါ်ပြီး ရလဒ်ကို print ထုတ်ပါတယ်။ အပိုင်း ၅ ကို အသစ်ထည့်ထားပါတယ်။ key=၊ default= နဲ့ empty input ရဲ့ အပြုအမူကို ပြပါတယ်။ str(123) ကို !r နဲ့ ပြထားလို့ ရလဒ်က string ဖြစ်နေတာကို quote နဲ့ မြင်ရပါမယ်။

Built-inဘာလုပ်လဲ
print()Value တွေကို screen (stdout) ပေါ် ထုတ်ပြတယ်
len()String၊ list၊ dict စတာတွေရဲ့ item အရေအတွက်ကို ပြန်ပေးတယ်
type()Value ရဲ့ data type ကို ပြန်ပေးတယ်
max(), min()အကြီးဆုံး / အသေးဆုံး value ကို ပြန်ပေးတယ်
sum()Number တွေ အားလုံးကို ပေါင်းပေးတယ်
sorted()စီပြီးသား list အသစ်ကို ပြန်ပေးတယ်
builtins_tour.py
# ===== 1. Basic Built-in Functions =====
numbers = [10, 20, 5, 40]

print("===== Basic Functions =====")
print(f"Length: {len(numbers)}")       # 4
print(f"Max: {max(numbers)}")           # 40
print(f"Min: {min(numbers)}")          # 5
print(f"Sum: {sum(numbers)}")           # 75
print(f"Sorted: {sorted(numbers)}")     # [5, 10, 20, 40]

# ===== 2. Type Conversion =====
print(f"\n===== Type Conversion =====")
print(f"int('5'): {int('5')}")
print(f"float('3.14'): {float('3.14')}")
print(f"str(123): {str(123)!r}")
print(f"type(123): {type(123)}")

# ===== 3. String Functions =====
print(f"\n===== String Functions =====")
text = "Python"
print(f"len('{text}'): {len(text)}")
print(f"max('{text}'): {max(text)}")
print(f"min('{text}'): {min(text)}")

# ===== 4. Advanced Functions =====
print(f"\n===== Advanced Functions =====")

# map()
nums = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, nums))
print(f"map(lambda x: x**2, [1,2,3,4]): {squares}")

# filter()
evens = list(filter(lambda x: x % 2 == 0, nums))
print(f"filter(lambda x: x % 2 == 0, [1,2,3,4]): {evens}")

# zip()
names = ["Sai", "Aye"]
ages = [25, 22]
combined = list(zip(names, ages))
print(f"zip(['Sai','Aye'], [25,22]): {combined}")

# enumerate()
for i, name in enumerate(names):
    print(f"enumerate: {i} = {name}")

# ===== 5. Keys, Defaults and Empty Input =====
print(f"\n===== Keys and Defaults =====")
words = ["kiwi", "fig", "banana"]
print(f"sorted(words, key=len): {sorted(words, key=len)}")
print(f"max(words, key=len): {max(words, key=len)}")
print(f"max([], default=0): {max([], default=0)}")
print(f"sum([]): {sum([])}, any([]): {any([])}, all([]): {all([])}")

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

  1. repr of a converted valueprint(f"str(123): {str(123)!r}")

    !r က value ရဲ့ repr() ကို ပြပါတယ်။ ဒါကြောင့် '123' လို့ quote ပါပြီး number မဟုတ်ဘဲ string ဖြစ်ကြောင်း ထင်ရှားပါတယ်။ !r မပါရင် 123 ပဲ ပေါ်လို့ int နဲ့ ခွဲမရပါဘူး။

  2. max() on a stringprint(f"max('{text}'): {max(text)}")

    String က character တွေရဲ့ sequence ဖြစ်လို့ max() က Unicode code point အကြီးဆုံး character ကို ရွေးပါတယ်။ Lowercase 'y' (121) က uppercase 'P' (80) ထက် ကြီးလို့ y ထွက်ပြီး min() က P ကို ပြန်ပေးပါတယ်။

  3. map() needs list()squares = list(map(lambda x: x**2, nums))

    map() က lazy iterator ကို ပြန်ပေးလို့ list() နဲ့ ပတ်ထားမှ value တွေကို ချက်ချင်း တွက်ပြီး print လုပ်လို့ရတဲ့ list ဖြစ်လာပါတယ်။ list() မပါရင် <map object ...> လို့ပဲ ပေါ်ပါမယ်။

  4. enumerate() counts for youfor i, name in enumerate(names):

    enumerate() က (index, item) tuple တွေကို ထုတ်ပေးလို့ counter variable ကို ကိုယ်တိုင် i += 1 လုပ်စရာ မလိုပါဘူး။ 1 ကနေ စချင်ရင် enumerate(names, start=1) လို့ ရေးပါ။

  5. key= returns the itemprint(f"max(words, key=len): {max(words, key=len)}")

    len နဲ့ နှိုင်းယှဉ်ပေမဲ့ ပြန်ပေးတာက 'banana' ဆိုတဲ့ စကားလုံးကိုယ်တိုင်ပါ၊ 6 မဟုတ်ပါဘူး။ sorted(words, key=len) မှာလည်း အရှည်အလိုက် စီထားတဲ့ စကားလုံးတွေကိုပဲ ရပါတယ်။

  6. Empty-input defaultsprint(f"max([], default=0): {max([], default=0)}")

    default=0 မပါရင် ဒီ line က ValueError: max() iterable argument is empty ဖြစ်ပါမယ်။ Report မှာ data မရှိသေးတဲ့ ရက်တွေ ရှိနိုင်ရင် default ကို အမြဲ စဉ်းစားပါ။

You should see
===== Basic Functions =====
Length: 4
Max: 40
Min: 5
Sum: 75
Sorted: [5, 10, 20, 40]

===== Type Conversion =====
int('5'): 5
float('3.14'): 3.14
str(123): '123'
type(123): <class 'int'>

===== String Functions =====
len('Python'): 6
max('Python'): y
min('Python'): P

===== Advanced Functions =====
map(lambda x: x**2, [1,2,3,4]): [1, 4, 9, 16]
filter(lambda x: x % 2 == 0, [1,2,3,4]): [2, 4]
zip(['Sai','Aye'], [25,22]): [('Sai', 25), ('Aye', 22)]
enumerate: 0 = Sai
enumerate: 1 = Aye

===== Keys and Defaults =====
sorted(words, key=len): ['fig', 'kiwi', 'banana']
max(words, key=len): banana
max([], default=0): 0
sum([]): 0, any([]): False, all([]): True

Loop ကိုယ်တိုင်ရေးမလား built-in သုံးမလား

Built-in တွေက loop ကို ဖျောက်ပေးရုံ မဟုတ်ပါဘူး။ ရည်ရွယ်ချက်ကို နာမည်နဲ့ ပြောပြပါတယ်။ any(x < 0 for x in values) ကို ဖတ်တဲ့သူက "negative ရှိလား" လို့ ချက်ချင်း နားလည်ပါတယ်။ Flag variable နဲ့ break ပါတဲ့ loop ခြောက်ကြောင်းကို ဖတ်ရင်တော့ အချိန်ယူရပါတယ်။ any() နဲ့ all() က အဖြေသိတာနဲ့ ရပ်လိုက်လို့ (short-circuit) generator expression နဲ့ တွဲရင် item အားလုံးကို မတွက်ပါဘူး။

ဒါပေမဲ့ built-in တွေကို အတင်း ဆက်တိုက် ချိတ်ရေးရင် ဖတ်ရခက်သွားနိုင်ပါတယ်။ list(map(lambda ...)) ထက် list comprehension က Python မှာ ပိုရိုးရှင်းတဲ့ အခါတွေ များပါတယ်။ ရည်ရွယ်ချက်ကို တစ်ချက်ကြည့်ရုံနဲ့ နားလည်ရင် built-in ကို သုံးပြီး logic ရှုပ်ရင် ရိုးရိုး loop ကို ရွေးပါ။

sorted() နဲ့ list.sort() ကွာခြားချက်

sorted(items) က list အသစ်ကို ပြန်ပေးပြီး မူရင်းကို မထိပါဘူး။ ဘယ် iterable ကိုမဆို လက်ခံပါတယ်။ items.sort() ကတော့ list ကို နေရာမှာတင် ပြင်ပြီး None ကို ပြန်ပေးပါတယ်။ ဒါကြောင့် result = items.sort() လို့ ရေးရင် result က None ဖြစ်သွားပါတယ်။

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

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

Code review တွေမှာ အများဆုံးတွေ့ရတဲ့ အကြံပြုချက်တစ်ခုက "ဒီ loop ကို built-in နဲ့ အစားထိုးလို့ရတယ်" ဆိုတာပါ။ Sales report တစ်ခုမှာ စုစုပေါင်းအတွက် sum()၊ ရောင်းအားအများဆုံး ပစ္စည်းအတွက် max(products, key=...)၊ ranking table အတွက် sorted(..., reverse=True) နဲ့ enumerate(start=1) ကို သုံးလိုက်ရင် loop သုံးခုလောက် ပျောက်သွားပြီး code က report ရဲ့ ဖော်ပြချက်လိုပဲ ဖတ်လို့ရလာပါတယ်။

အရေးကြီးတာက edge case တွေအတွက် test ရေးထားဖို့ပါ။ ရောင်းအား မရှိတဲ့နေ့ (empty list)၊ အမှတ်တူနေတဲ့ ပစ္စည်းနှစ်ခု (tie) နဲ့ ထပ်နေတဲ့ data တွေမှာ function က ဘာပြန်ပေးရမလဲဆိုတာ ကြိုဆုံးဖြတ်ပြီး test လုပ်ပါ။ Tie ဖြစ်ရင် max() က ပထမဆုံးတွေ့တဲ့ item ကို ပြန်ပေးတယ်ဆိုတာကိုလည်း documentation မှာ ရေးထားသင့်ပါတယ်။ Linter တွေ (ဥပမာ Ruff ရဲ့ builtins rule တွေ) က built-in နာမည်ကို shadow လုပ်တာကို အလိုအလျောက် ဖမ်းပေးနိုင်ပါတယ်။

အမှားများ

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

Built-in နာမည်ကို variable နာမည်အဖြစ် သုံးတာ

list၊ dict၊ str၊ sum၊ max၊ id၊ input စတဲ့ နာမည်တွေကို variable အဖြစ် သုံးရင် အဲဒီ scope ထဲမှာ built-in ကို ဖုံးပစ်ပါတယ်။ prices၊ total၊ user_id လို ဖော်ပြချက်ပါတဲ့ နာမည်ကို သုံးပါ။ Interactive shell မှာ မတော်တဆ ဖုံးမိရင် del list နဲ့ ပြန်ဖော်လို့ ရပါတယ်။

Iterator ကို နှစ်ခါ loop ပတ်တာ

pairs = zip(names, ages) လို့ သိမ်းပြီး ပထမ loop မှာ print လုပ်၊ ဒုတိယ loop မှာ dict(pairs) လုပ်ရင် dict က ဗလာ ဖြစ်နေပါမယ်။ Error မတက်လို့ ပိုအန္တရာယ်များပါတယ်။ နှစ်ခါသုံးမယ်ဆိုရင် pairs = list(zip(names, ages)) လို့ ရေးပါ။

max() ကို ဗလာ data နဲ့ ခေါ်တာ

Filter လုပ်ပြီးမှ max() ခေါ်တဲ့ code က test data မှာ အလုပ်လုပ်ပေမဲ့ filter နဲ့ ကိုက်တဲ့ item မရှိတဲ့နေ့မှာ ValueError နဲ့ crash ဖြစ်ပါတယ်။ default=None ပေးပြီး None ကို caller ဘက်မှာ ကိုင်တွယ်ပါ။

Mini challenge

Score report ကို built-in တွေနဲ့ပဲ ရေးပါ

{name: score} dict တစ်ခုကို လက်ခံပြီး report dict ကို ပြန်ပေးတဲ့ summarize() နဲ့ ranking ကို နံပါတ်တပ်ပြီး print ထုတ်တဲ့ print_ranking() ကို ရေးပါ။ summarize() ထဲမှာ for loop ကိုယ်တိုင် မရေးဘဲ built-in တွေနဲ့ generator expression တွေပဲ သုံးပါ။

  • count၊ top (အမှတ်အများဆုံးလူ)၊ average (ဒသမ တစ်နေရာ)၊ all_passed၊ any_perfect၊ ranking ဆိုတဲ့ key တွေ ပါရပါမယ်
  • Empty dict ဆိုရင် crash မဖြစ်ဘဲ top နဲ့ average က None ဖြစ်ရပါမယ်
  • print_ranking() က enumerate(start=1) နဲ့ 1 ကနေ စပြီး နံပါတ်တပ်ရပါမယ်
score_report.py
def summarize(scores: dict[str, int], pass_mark: int = 50) -> dict:
    """Describe a score table using built-ins only (no explicit loops)."""
    # TODO: count       -> how many students
    # TODO: top         -> name with the highest score (None when empty)
    # TODO: average     -> rounded to 1 decimal (None when empty)
    # TODO: all_passed  -> every score >= pass_mark
    # TODO: any_perfect -> at least one score of 100
    # TODO: ranking     -> names, best first
    return {}


def print_ranking(scores: dict[str, int]) -> None:
    # TODO: sort (name, score) pairs by score, best first
    # TODO: print "  1. Name score" lines, numbered from 1
    pass


class_a = {"Aye": 82, "Sai": 47, "Mya": 91, "Hla": 66}
print(summarize(class_a))
print_ranking(class_a)
print(summarize({}))

ရလဒ်: class_a အတွက် top က 'Mya'၊ average က 71.5၊ all_passed က False ဖြစ်ပြီး ranking လေးကြောင်း ထွက်ပါမယ်။ Empty dict အတွက် top နဲ့ average က None၊ all_passed က True ဖြစ်ပါမယ်။

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

max(scores, key=scores.get, default=None) က dict ရဲ့ key တွေကို value နဲ့ နှိုင်းယှဉ်ပါတယ်။ Average အတွက် dict ဗလာ ဖြစ်မဖြစ် if scores else None နဲ့ အရင်စစ်ပါ။

အဖြေ ကြည့်ရန်
score_report.py
def summarize(scores: dict[str, int], pass_mark: int = 50) -> dict:
    """Describe a score table using built-ins only (no explicit loops)."""
    values = scores.values()
    return {
        "count": len(scores),
        "top": max(scores, key=scores.get, default=None),
        "average": round(sum(values) / len(scores), 1) if scores else None,
        "all_passed": all(v >= pass_mark for v in values),
        "any_perfect": any(v == 100 for v in values),
        "ranking": sorted(scores, key=scores.get, reverse=True),
    }


def print_ranking(scores: dict[str, int]) -> None:
    ranking = sorted(scores.items(), key=lambda item: item[1], reverse=True)
    for place, (name, score) in enumerate(ranking, start=1):
        print(f"  {place}. {name:<4} {score}")


class_a = {"Aye": 82, "Sai": 47, "Mya": 91, "Hla": 66}
print(summarize(class_a))
print_ranking(class_a)
print(summarize({}))

Quick Quiz 1 / 3

words = ["kiwi", "fig", "banana"] ဆိုရင် max(words, key=len) က ဘာပြန်ပေးမလဲ။

Quick Quiz 2 / 3

pairs = zip(["a", "b"], [1, 2]) ပြီးနောက် print(list(pairs)) နဲ့ print(dict(pairs)) ကို ဆက်တိုက် run ရင် ဒုတိယ line က ဘာထုတ်မလဲ။

Quick Quiz 3 / 3

Report function မှာ scores list က ဗလာဖြစ်နိုင်ပါတယ်။ အမြင့်ဆုံးအမှတ်ကို ဘယ်လို ရယူသင့်လဲ။

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

  • Built-in တွေကို inspect၊ convert၊ aggregate၊ order၊ iterate ဆိုပြီး အုပ်စုခွဲမှတ်ပါ
  • sum([]) က 0၊ all([]) က True၊ max([]) က ValueError ဖြစ်ပြီး default= နဲ့ ကာကွယ်နိုင်ပါတယ်
  • key= က နှိုင်းယှဉ်ပုံကို ပြောင်းပေမဲ့ မူရင်း item ကိုပဲ ပြန်ပေးပါတယ်
  • map/filter/zip/enumerate က တစ်ခါသုံး iterator ဖြစ်ပြီး list, sum, str လို နာမည်တွေကို shadow မလုပ်ပါနဲ့

The Python Tutorial — Python Software Foundation

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

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

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

Built-in Functions (Enhanced) | Thuta Learning