🏠 Home / Hub

🧩 Python Lesson 05 — Functions & Modules

← Back to Python Menu

1. Function ဆိုတာ

ထပ်ခါထပ်ခါသုံးမယ့် code ကို နာမည်ပေးပြီး သိမ်းထားတာ function ပါ။ Reuse လုပ်လို့ရပြီး code ကိုဖတ်ရလွယ်စေတယ်။

def say_hello():
    print("Hello Python")

say_hello()

2. Parameters & Return

def add(a, b):
    return a + b

total = add(10, 20)
print(total)

def greet(name, city):
    return f"{name} lives in {city}"

print(greet("Mg Mg", "Yangon"))

3. Default Values

def make_profile(name, role="student"):
    return f"{name} is a {role}"

print(make_profile("Su Su"))
print(make_profile("Ko Ko", "developer"))

4. *args and **kwargs

def sum_all(*numbers):
    total = 0
    for n in numbers:
        total += n
    return total

print(sum_all(1, 2, 3, 4))

def show_user(**user):
    for key, value in user.items():
        print(key, value)

show_user(name="Aung", age=22, city="Mandalay")

5. Modules

import math
print(math.sqrt(25))

from datetime import datetime
print(datetime.now())

import random
print(random.randint(1, 10))
ကိုယ်ပိုင် file တစ်ခုထဲမှာ function ရေးထားရင်လည်း import my_file နဲ့ပြန်သုံးလို့ရတယ်။

6. Clean Function Example

def calculate_discount(price, percent):
    discount = price * percent / 100
    final_price = price - discount
    return final_price

print(calculate_discount(50000, 10))

📌 Study Checklist