Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingbeginner

Lambda Functions (In Depth)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

⚡ Lesson 62: Python Lambda Functions (Anonymous Functions)

1. Lambda Function ဆိုတာဘာလဲ?

မြန်မာ → Lambda function ဆိုတာ နာမည်မပေးထားတဲ့ function (anonymous function) တစ်မျိုး ဖြစ်ပြီး, lambda keyword သုံးပြီး တစ်ကြောင်းတည်းနဲ့ ရေးနိုင်တယ်။

English → A lambda function is an anonymous function defined using the lambda keyword, usually written in a single line.

2. Lambda Syntax

lambda arguments: expression

မြန်မာရှင်းချက်arguments ဆိုတာ parameter တွေ၊ expression က return value ဖြစ်မယ်။

3. အကျဉ်းချုပ်

✅ Lambda = anonymous function (no name)

✅ Syntax → lambda args: expression

✅ သုံးနိုင်တဲ့နေရာများ → map(), filter(), sorted()

✅ Short, one-time-use functions အတွက် အဆင်ပြေတယ်

python
# ===== 1. Basic Lambda =====
add = lambda x, y: x + y
print(f"add(5, 3) = {add(5, 3)}")

# ===== 2. Lambda vs Normal Function =====
print(f"\n===== Comparison =====")

def square(x):
    return x**2

square_lambda = lambda x: x**2

print(f"square(4) = {square(4)}")
print(f"square_lambda(4) = {square_lambda(4)}")

# ===== 3. Lambda with map() =====
print(f"\n===== Lambda with map() =====")

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

# ===== 4. Lambda with filter() =====
print(f"\n===== Lambda with filter() =====")

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

# ===== 5. Lambda with sorted() =====
print(f"\n===== Lambda with sorted() =====")

data = [("Sai", 25), ("Aye", 20), ("Mya", 30)]
sorted_data = sorted(data, key=lambda x: x[1])
print(f"Sorted by age: {sorted_data}")

# ===== 6. Lambda with reduce() =====
print(f"\n===== Lambda with reduce() =====")

from functools import reduce
nums = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, nums)
print(f"Product: {product}")
You should see
add(5, 3) = 8 ===== Comparison ===== square(4) = 16 square_lambda(4) = 16 ===== Lambda with map() ===== Squares: [1, 4, 9, 16, 25] ===== Lambda with filter() ===== Even numbers: [2, 4, 6] ===== Lambda with sorted() ===== Sorted by age: [('Aye', 20), ('Sai', 25), ('Mya', 30)] ===== Lambda with reduce() ===== Product: 24
Lambda Functions (In Depth) | Thuta Learning