ထပ်ခါထပ်ခါသုံးမယ့် code ကို နာမည်ပေးပြီး သိမ်းထားတာ function ပါ။ Reuse လုပ်လို့ရပြီး code ကိုဖတ်ရလွယ်စေတယ်။
def say_hello():
print("Hello Python")
say_hello()
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"))
def make_profile(name, role="student"):
return f"{name} is a {role}"
print(make_profile("Su Su"))
print(make_profile("Ko Ko", "developer"))
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")import math print(math.sqrt(25)) from datetime import datetime print(datetime.now()) import random print(random.randint(1, 10))
import my_file နဲ့ပြန်သုံးလို့ရတယ်။
def calculate_discount(price, percent):
discount = price * percent / 100
final_price = price - discount
return final_price
print(calculate_discount(50000, 10))