Data Structures (ဒေတာ ဖွဲ့စည်းပုံများ)
Data အများကြီးကို Variable တစ်ခုချင်းစီ ကြေညာပြီး မသိမ်းနိုင်ပါဘူး — ဒါကြောင့် Data Structure တွေကိုသုံးပြီး စီမံကြပါမယ်။
၁. Lists (စာရင်းများ)
Section titled “၁. Lists (စာရင်းများ)”List ဆိုတာ Data တွေကို အစဉ်လိုက် (Ordered) စီပြီး [] ထဲမှာ သိမ်းထားတာပါ။ ပြင်လို့ ရပါတယ် (Mutable)။
fruits = ["Apple", "Banana", "Orange"]
# Index နဲ့ ဝင်ကြည့်ခြင်း (0 ကနေ စပါတယ်)print(fruits[0]) # Appleprint(fruits[-1]) # Orange (နောက်ဆုံး)
# ထည့်ခြင်းfruits.append("Mango") # နောက်ဆုံးမှာ ထည့်fruits.insert(1, "Grape") # Index 1 မှာ ထည့်
# ဖျက်ခြင်းfruits.remove("Banana") # တန်ဖိုးနဲ့ ရှာဖျက်popped = fruits.pop() # နောက်ဆုံးကောင် ဖြုတ်ထုတ်fruits.pop(0) # Index နဲ့ ဖျက်
# စစ်ဆေးခြင်းprint(len(fruits)) # အရေအတွက်print("Apple" in fruits) # True/False
# Sort လုပ်ခြင်းfruits.sort() # မူရင်း list ကို Sortsorted_fruits = sorted(fruits) # မူရင်း list မပြောင်းဘဲ အသစ် ပြန်ပေးSlicing
Section titled “Slicing”numbers = [0, 1, 2, 3, 4, 5]print(numbers[1:4]) # [1, 2, 3] (Index 1 ကနေ 3 အထိ)print(numbers[:3]) # [0, 1, 2] (အစကနေ)print(numbers[3:]) # [3, 4, 5] (Index 3 ကနေ ဆုံးအထိ)print(numbers[::2]) # [0, 2, 4] (တစ်ခုကျော် တစ်ခု)print(numbers[::-1]) # [5, 4, 3, 2, 1, 0] (ပြောင်းပြန်)Built-in Functions နဲ့ List
Section titled “Built-in Functions နဲ့ List”scores = [85, 92, 60, 78, 95, 70]
print(len(scores)) # 6 (အရေအတွက်)print(sum(scores)) # 480 (ပေါင်းခြင်း)print(min(scores)) # 60 (အငယ်ဆုံး)print(max(scores)) # 95 (အကြီးဆုံး)print(sum(scores) / len(scores)) # 80.0 (ပျမ်းမျှ)print(sorted(scores)) # [60, 70, 78, 85, 92, 95]
# ခြုံငုံစစ်ဆေးခြင်းprint(any(s >= 90 for s in scores)) # True (တစ်ခုမှ 90+ ရှိသလား?)print(all(s >= 60 for s in scores)) # True (အကုန် 60+ လား?)၂. Dictionaries (အဘိဓာန်များ)
Section titled “၂. Dictionaries (အဘိဓာန်များ)”Dictionary က Data တွေကို Key-Value (ကုဒ်-တန်ဖိုး) အတွဲနဲ့ {} ထဲ သိမ်းပါတယ်။ Key ကို သုံးပြီး Value ကို ချက်ချင်း ရနိုင်ပါတယ်:
student = { "name": "Aung Aung", "age": 20, "major": "Computer Science", "gpa": 3.8}
# Value ရယူခြင်းprint(student["name"]) # Aung Aung
# ✅ Safe Access — Key မရှိရင် Default ပြန်ပေး (.get() ကိုသုံးပါ)print(student.get("phone", "မသိ")) # မသိ (phone key မရှိသောကြောင့်)print(student.get("name", "")) # Aung Aung
# Update / Addstudent["age"] = 21 # Key ရှိပြီးသားဆိုရင် Updatestudent["city"] = "ရန်ကုန်" # Key မရှိသေးဘူးဆိုရင် ထည့်
# Deletedel student["gpa"] # Key ကိုဖျက်# Loop ပတ်ခြင်းfor key, value in student.items(): print(f"{key}: {value}")
# Key တွေ / Value တွေ ချည်းပဲprint(list(student.keys())) # ['name', 'age', 'major', 'city']print(list(student.values())) # ['Aung Aung', 21, 'Computer Science', 'ရန်ကုန်']
# Key ရှိမရှိ စစ်ဆေးခြင်းif "name" in student: print("name ရှိတယ်")Nested Data Structures — List of Dictionaries
Section titled “Nested Data Structures — List of Dictionaries”လက်တွေ့ Program တွေမှာ Data ဟာ ဒီပုံစံ ဖြစ်တတ်ပါတယ် (API Response ပုံစံနဲ့ တူပါတယ်):
students = [ {"name": "Aung Aung", "score": 85, "grade": "B"}, {"name": "Su Su", "score": 92, "grade": "A"}, {"name": "Kyaw Kyaw", "score": 70, "grade": "C"},]
# Loop ပတ်ပြီး ဖတ်ခြင်းfor student in students: print(f"{student['name']}: {student['score']} ({student['grade']})")
# Score 80+ ကျောင်းသားများ ရှာခြင်းhigh_scorers = [s["name"] for s in students if s["score"] >= 80]print(high_scorers) # ['Aung Aung', 'Su Su']
# Average Scoreaverage = sum(s["score"] for s in students) / len(students)print(f"ပျမ်းမျှ: {average:.1f}")၃. Tuples
Section titled “၃. Tuples”Tuple က List နဲ့ တူပြီး () ကို သုံးပါတယ်။ အဓိကကွာတာက တစ်ကြိမ် ဆောက်ပြီးရင် ပြင်လို့ မရပါ (Immutable):
coordinates = (16.8661, 96.1951) # Latitude, Longitudergb = (255, 128, 0) # Color values
print(coordinates[0]) # 16.8661print(coordinates[1]) # 96.1951
# Unpack လုပ်ခြင်းlat, lon = coordinatesprint(f"Latitude: {lat}, Longitude: {lon}")
# coordinates[0] = 0 # ← TypeError! ပြင်လို့ မရပါဘူးဘာကြောင့် Tuple သုံးတာလဲ?
- List ထက် ပိုမြန်ပါတယ် (ပြင်လို့ မရတာကြောင့် Python က Optimize လုပ်ထားပါတယ်)
- ပြောင်းလဲသွားလို့ မဖြစ်တဲ့ Data (မြေပုံနေရာ, RGB Color, Record) တွေ သိမ်းဖို့ သင့်တော်ပါတယ်
- Function ကနေ Values ပြန်ပေးတဲ့အခါ Tuple အနေနဲ့ Return ပေးပါတယ်
၄. Sets
Section titled “၄. Sets”Set က တူညီတဲ့ Value ၂ ခု မထားနိုင်ပါဘူး (Unique Values Only):
numbers = {1, 2, 2, 3, 4, 4, 5}print(numbers) # {1, 2, 3, 4, 5} (Duplicate တွေကို အလိုလို ဖျက်ပါတယ်)
# ထည့် / ဖျက်numbers.add(6)numbers.remove(3)
# Set Operations (သင်္ချာ Set ကဲ့သို့)a = {1, 2, 3, 4}b = {3, 4, 5, 6}print(a & b) # {3, 4} (နှစ်ဘက်လုံးမှာပါတာ — Intersection)print(a | b) # {1,2,3,4,5,6} (ပေါင်းပြီးသား — Union)print(a - b) # {1, 2} (a မှာသာပါတာ — Difference)Set ရဲ့ အများကြိုက် Use Case:
# List ထဲကနေ Duplicate ဖြုတ်ခြင်းnames = ["Aung", "Su", "Aung", "Kyaw", "Su"]unique_names = list(set(names))print(unique_names) # ['Kyaw', 'Aung', 'Su']
# Membership Check — List ထက် Set ပိုမြန်ပါတယ်valid_users = {"admin", "manager", "user"}role = "admin"if role in valid_users: print("Access Granted")ဘယ် Data Structure ကို ဘယ်အချိန် ရွေးမလဲ?
Section titled “ဘယ် Data Structure ကို ဘယ်အချိန် ရွေးမလဲ?”| Data Structure | ရွေးချယ်သင့်သောအချိန် | ဥပမာ |
|---|---|---|
| List | အစဉ်လိုက် ဖြစ်ရမယ်၊ ပြင်ရမယ်၊ ထပ်တဲ့တန်ဖိုး ပါနိုင်တယ် | မှာထားသော Items, ကျောင်းသားစာရင်း |
| Dict | Key-Value တွဲနဲ့ ရှာနိုင်ရမယ် | User Profile, Settings Config |
| Tuple | ပြောင်းလဲမှာ မဟုတ်တဲ့တန်ဖိုး | GPS Coordinates, RGB |
| Set | Unique တန်ဖိုးသာ၊ Duplicate မလိုဘူး | Login Sessions, Tag List |
ဖြစ်တတ်တဲ့ အမှားများ
Section titled “ဖြစ်တတ်တဲ့ အမှားများ”# ၁. Dict Key Errord = {"name": "Aung"}# print(d["age"]) # KeyError!print(d.get("age", 0)) # 0 (Safe ဖြစ်ပါတယ်)
# ၂. List Index Errorlst = [1, 2, 3]# print(lst[5]) # IndexError!if len(lst) > 5: print(lst[5])
# ၃. Empty dict နဲ့ empty set — ကွာခြားချက်empty_dict = {} # ← Dict (မှန်ပါတယ်)empty_set = set() # ← Set (မှန်ပါတယ်) — {} မသုံးပါနဲ့ (Dict ဖြစ်သွားပါမယ်)