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

Python Tuples

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

Tuple vs Other Collections
Tuple က ordered ဖြစ်ပေမယ့် immutable ဖြစ်တာကို collection comparison နဲ့တွဲမြင်နိုင်ပါတယ်။

🐍 Lesson 13: Python Tuples (Python မပြောင်းလဲနိုင်တဲ့ စာရင်း)

1. Tuple ဆိုတာဘာလဲ?

မြန်မာ → Tuple ဆိုတာ List လိုပဲ တန်ဖိုးအများစုကို သိမ်းထားနိုင်တဲ့ Data Type ဖြစ်ပေမယ့် immutable (မပြောင်းလဲနိုင်) တယ်။

English → A tuple is like a list, but it is immutable (cannot be changed after creation).

2. Tuple Unpacking

Tuple ထဲက တန်ဖိုးတွေကို တိုက်ရိုက် variable အများထဲ ခွဲထည့်နိုင်တယ်။

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

✅ Tuple = ordered + immutable collection

✅ Indexing, slicing နဲ့ တန်ဖိုးခေါ်နိုင်

✅ တန်ဖိုးပြောင်းချင်ရင် → List ပြောင်းပြီး ပြန် Tuple ပြန်လုပ်

✅ One-item tuple မှာ comma လိုအပ်

python
# ===== 1. Create a Tuple =====
fruits = ("apple", "banana", "cherry")
print(f"Tuple: {fruits}")
print(f"Type: {type(fruits)}")

# ===== 2. Indexing & Slicing =====
print(f"\n===== Indexing & Slicing =====")
print(f"First item: {fruits[0]}")
print(f"Last item: {fruits[-1]}")
print(f"Slice [0:2]: {fruits[0:2]}")

# ===== 3. Tuple is Immutable =====
print(f"\n===== Immutable =====")
# fruits[1] = "mango"  # ❌ Error: 'tuple' object does not support item assignment

# To change: Convert to list, modify, convert back
y = list(fruits)
y[1] = "mango"
fruits = tuple(y)
print(f"After conversion: {fruits}")

# ===== 4. Tuple Length =====
print(f"\nLength: {len(fruits)}")

# ===== 5. One-item Tuple =====
print(f"\n===== One-item Tuple =====")
a = ("apple",)   # ✅ Tuple (comma needed)
b = ("apple")    # ❌ String (no comma)
print(f"Type of a: {type(a)}")
print(f"Type of b: {type(b)}")

# ===== 6. Tuple Unpacking =====
print(f"\n===== Unpacking =====")
colors = ("red", "green", "blue")
(r, g, b) = colors
print(f"Red: {r}, Green: {g}, Blue: {b}")

# ===== 7. Loop Through Tuple =====
print(f"\n===== Looping =====")
for item in ("apple", "banana", "cherry"):
    print(item)
You should see
Tuple: ('apple', 'banana', 'cherry') Type: ===== Indexing & Slicing ===== First item: apple Last item: cherry Slice [0:2]: ('apple', 'banana') ===== Immutable ===== After conversion: ('apple', 'mango', 'cherry') Length: 3 ===== One-item Tuple ===== Type of a: Type of b: ===== Unpacking ===== Red: red, Green: green, Blue: blue ===== Looping ===== apple banana cherry
Python Tuples | Thuta Learning