Python တွင် tuple နှင့် list ဟူ၍ sequence structure နှစ်မျိုးရှိသည်။ Empty tuple နှင့် empty list တို့ထဲတွင် ရှိသည့် element များအားလုံးသည် Python object များ ဖြစ်ကြသည်။
Python တွင် tuple နှင့် list ကို object များ အစဥ◌်တကျ နေရာတကျ ထည့်ထားနိုင်သည့် ကွန်တိန်နာ(ordered container)များ အဖြစ် မှတ်ယူနိုင်သည်။ Dictionary ထဲတွင် သိမ်းဆည်းသည့် ဒေတာများသည် အစီအစဉ်တကျ သိုလှောင်ထားခြင်းမရှိပါ။ (Dictionaries do not store information in any particular order)။ Tuple ၏ အဓိပ္ပာယ်(definition)မှာ ကော်မာဖြင့် ခွဲခြားထားသည့် တန်ဖိုး အစုအဝေး(collection of comma-separated values)တစ်ခု ဖြစ်သည်။ ဖြစ်သည်။
Tuple သည် list နှင့် များစွာ တူညီသည်။ သို့သော် မတူညီသည့် အဓိက အချက်မှာ tuple များသည် ပြင်၍ ပြောင်း၍မရသည့်(immutable) data structure ဖြစ်သည်။ Immutable ဆိုသည်မှာ အသစ်ထပ်ထည့်ခြင်း၊ ရှိပြီးသား item များကို ဖျက်ပစ်ခြင်း လုပ်ခွင့်မပြုပါ။ Tuple ကို ပြောင်းလဲ၍ မရသည့် အသေဖြစ်သည့် (constant) list တစ်မျိုးဟု သတ်မှတ်သည်။ ဥပမာ- နှစ် တစ်နှစ်အတွင်းရှိ လနာမည်များ၊ ရက်နာမည်များ သည် မည်သည့်အခါမျှ ပြောင်းရန် လိုအပ်လိမ့်မည် မဟုတ်ပေ။ တစ်ဦးတစ်ယောက်ကြောင့် မှားယွင်းပြီး ပြင်ဆင်လိုက်ခြင်း မဖြစ်စေလိုသည့် အရာများ၊ ဒေတာများကို tuple အဖြစ် ပြုလုပ်ထားပြီး ကာကွယ်နိုင်သည်။
Tuple ထဲရှိ element များကို add လု်ပ၍သော်လည်းကောင်း, remove, လုပ်၍သော်လည်းကောင်း, replace လုပ်၍သော်လည်းကောင်း မရနိုင်ပါ။ သို့သော် tuple ထဲတွင်ရှိ element များ ကိုယ်တိုင်သည် mutable data အမျိုးအစားများ ဖြစ်နိုင်သည်။
ဥပမာ tuple တစ်ခုထဲတွင် list တစ်ခု ပါဝင်နေနိုင်သည်။ Tuple ထဲက list သည် mutable data အမျိုးအစား ဖြစ်သောကြောင့် ပြောင်းနိုင်ပြင်နိုင်သည်။ Tuple အသုံးပြုရသည် အဓိက အားသာချက်များမှာ
Tuple အသစ်တစ်ခု တည်ဆောက်နိုင်သည့် နည်းများ(creating new tuples)
(၁) လက်သည်းကွင်း () ကို အသုံးပြု၍ ကော်မာခံရေး၍လည်း tuple ပြုလုပ်နိုင်သည်။
(၂) tuple() ကို အသုံးပြု၍ tuple ပြုလုပ်နိုင်သည်။
# Tuple တည်ဆောက်နည်း (creating new tuples)
x = (1, 2, 3)
x = 1, 2, 3
x = 2, # the comma tells Python it's a tuple
print(x, type(x))
empty_tuple = () # Empty tuple အသစ် တစ်ခု တည်ဆောက်သည်။
print(empty_tuple)
print(type(empty_tuple)) # type() ဖြင့် အမျိုးအစားကို စစ်ဆေးသည်။
x = (0,1,2,3,4,5,6,7,8,9) # Integer tuple အသစ် တစ်ခု တည်ဆောက်သည်။
x
pet1 = [ 'dog', 'cat', 'parrot']
print(type(pet1))
pet2 = tuple(pet1) # ဖြင့် ရှိပြီးသား list တစ်ခုကို tuple တစ်ခု အဖြစ်သို့ပြောင်းသည်။
print(type(pet2))
Tuple နှင့် list ထဲတွင် ဘာမျှမရှိသည့် empty tuple နှင့် empty list တို့ ဖြစ်နိုင်သည်။
Element တစ်ခုထက်ပိုများသည့် tuple ပြုလုပ်ရန်အတွက် လက်သည်းကွင်း ()ထဲတွင် element များကို ကော်မာခံ၍ ထည့်ရေးသည်။
pets = ('dog', 'cat', 'parrot')
pets
# လက်သည်းကွင်း ()မပါဘဲ element ၏ နောက်တွင် ကော်မာ ထည့်ရေး၍လည်း tuple ပြုလုပ်နိုင်သည်။
pets = 'dog',
pets
x = 2, # the comma tells Python it's a tuple
print(x, type(x))
# Element တစ်ခုထက်ပိုများပါက
# လက်သည်းကွင်း ()မပါဘဲ ကော်မာ(comma) ခံရေး၍လည်း tuple ပြုလုပ်နိုင်သည်။
pets = 'dog', 'cat', 'parrot'
pets
လက်သည်းကွင်း (parentheses) မပါဘဲ နောက်ဆုံး element ၏ နောက်တွင် ကော်မာ(comma) ထည့်ရေး၍လည်း tuple ပြုလုပ်နိုင်သည်။ သို့သော် လက်သည်းကွင်း (parentheses) ထဲတွင် ထည့်ရေးခြင်းကြောင့် ဖတ်ရလွယ်သည်။ ပရိုရမ်က output ထုတ်ပေးသည့်အခါ နားလည်မှု မရှုတ်ထွေးစေရန် လက်သည်းကွင်း (parentheses) ထဲတွင် ထည့်၍ အမြဲဖော်ပြလေ့ရှိသည်။
Tuple များကို မှတ်တမ်း မှတ်ရာများကို သိမ်းဆည်းရာအဖြစ် အသုံးပြုနိုင်သည်။ (Tuples as records)
#Latitude and longitude of the Los Angeles International Airport.
lax_coordinates = (33.9425, -118.408056)
#Data about Tokyo: name, year, population (millions), population change (%),area (km²).
city, year, pop, chg, area = ('Tokyo', 2003, 32450, 0.66, 8014)
x = ['pig', 'cow', 'horse']
a, b, c = x
print(a, b, c)
a, b, *rest = range(5)
a, b, rest
a, b, *rest = range(3)
a, b, rest
a, b, *rest = range(2)
a, b, rest
# * prefix
a, *body, c, d = range(5)# Index နံပါတ် 1, 3 နှင့် 4 ကို သက်ဆိုင်ရာ တို့ဖြင့် assign လုပ်ပြီး ကျန်ကိန်းများအားလုံးအတွက် *body
a, body, c, d
*head, b, c, d = range(5)
head, b, c, d
x = list()
y = ['a', 25, 'dog', 8.43]
tuple1 = (10, 20)
z = list(tuple1)
# list comprehension
a = [m for m in range(8)]
print(a)
b = [i**2 for i in range(10) if i>4]
print(b)
ပရိုဂရမ်က output တွင် လက်သည်းကွင်း (parentheses) ထဲတွင် ထည့်၍ အမြဲဖော်ပြရသည့်တခြားသော အကြောင်းမှာ nested tuple များကို ရှင်းလင်းထင်ရှားစွာ ဖတ်နိုင်ရန် ဖြစ်သည်။
pets = ('dog', 'cat'), 'parrot'
pets
('dog', 'cat') သည် main tuple ထဲတွင် ပါဝင်နေသည့် subtuple တစ်ခုဖြစ်သည်။ nested tuple ထဲတွင် tuple ပေါင်းများစွာ အကန့်အသတ်မရှိ ထည့်ထားနိုင်သည်။ သတိပြုရန်အချက်မှာ main tuple ထဲတွင် tuple အလွှာပေါင်းများစွာ ထည့်ထားခြင်းကြောင့် ဖတ်ရန် နှင့် tuple တည်ဆောက်ထားပုံ(structure)ကို နားလည်ရန် ခက်ခဲ့လိမ့်မည်။
x = 2 ဟုသိထားသည့်အခါ ထို x ၏ data type ကိုလည်း သတိပြုရန်လိုသည်။ အောက်က ဥပမာတွင် x သည် integer တစ်ခု ဖြစ်နိုင်သလို tuple တစ်ခုလည်းဖြစ်နိုင်သည်။ သို့သော် tuple ဖြစ်ကြောင်းထင်ရှားစေရန် လက်သည်းကွင်း() ထဲတွင် ဖော်ပြထားသည်။
x =(2)
print(x)
print(type(x))
x = (2,)
print(x)
print(type(x))
Tuple တွင် variable များစွာကို တပြိုက်နက် assign လုပ်နိုင်သည်။
vehicle = ('Toyota', 'BMW', 'Benz')
a, b, c = vehicle
a
a, b, c
Tuple အတွင်းရှိ element များကို နည်း နှစ်မျိုးဖြင့် access လုပ်နိုင်သည်။
# Accessing Tuple Elements Using Indexing
pets = 'dog', 'cat', 'parrot'
pets[1] # to access the second index:
မရှိသည့် element ကို access လုပ်လျှင် IndexError: tuple index out of range ပေးလိမ့်မည်။
pets[3] # IndexError: tuple index out of range
အနောက်မှ စတင်၍ ပြန်လည် ညွှန်ပြလိုလျှင် negative index များကို အသုံးပြုသည်။ -1 သည် နောက်ဆုံးအလုံး(last element in the tuple) ဖြစ်သည်။ -2 သည် ဒုတိယနောက်ဆုံးအလုံး(second from last element in the tuple ဖြစ်သည်။
pets = 'dog', 'cat', 'parrot', 'gerbil'
pets[-1]
pets[-2]
Tuple ၏ index နံပါတ်များသည် (list indices) ကိန်းပြည့်များ(integers)သာ ဖြစ်ရမည်။ ကိန်းပြည့်များ(integers) ခဲ့လျှင် TypeError ပေးလိမ့်မည်။
pets = 'dog', 'cat', 'parrot'
pets['1']
Tuple သည် immutable ဖြစ်သော်လည်း tuple ထဲတွင် ရှိနေသည့် list သည် mutable ဖြစ်သည်။ (tuples are immutable, but member objects may be mutable.)
x = (1, 2, 3)
# del(x[1]) # fails
# x[1] = 8 # fails
print(x)
y = ([1, 2], 3) # a tuple where the first item is a list
del(y[0][1]) # delete the 2
print(y) # the list within the tuple is mutable
y += (4,) # concatenating two tuples works
print(y)
Python ရှိ list, tuple, str နှင့် တခြားသော sequence type များအားလုံးအတွက် slicing operation ကို အသုံးပြုကြသည်။ Python ရှိ list, tuple, str နှင့် တခြားသော sequence type များအားလုံးအတွက် slicing operation ကို အသုံးပြုကြသည်။ slicing operation လုပ်သည့်အခါ နောင်ဆုံးအလုံး(last item)ကို ချန်ထားရသည့် အကြောင်းမှာ zero-based indexing ကြောင့် ဖြစ်သည်။
[Start index: Stop index: Increment]
l = [10, 20, 30, 40, 50, 60]
l[:2] # split at 2
l[2:]
l[:3] # split at 3
l[3:]
Start index ၊Stop index ၊ Increment တို့တန်ဖိုးများကို ဘာမှ မထည့်ပေးခြင်းသည် item အားလုံးကို ရည်ညွန်းသည်။
s = tuple(range (0,10))
s[ : : ]
# Item (၃)ခု မြောက် item ကိုယူရန်
s = tuple(range (0,10))
s[::3]
# နောက်မှ စ,သည်။ ပြောင်းပြန် ပြန်စီသည်။
s[::-1]
# နောက်မှ စ,၍ ပြောင်းပြန် ပြန်စီသည်။ (၂)ခု မြောက် item ကိုယူရန် ။
s[::-2]
indexing သည် element တစ်ခုချင်းစီ (individual element) ကိုသာ access လုပ်နိုင်သည်။ တစ်လုံးထက်ပိုများသည့် element များ(subset များ)ကို access လုပ်လိုသည်အခါ slicing ကို အသုံးပြုရသည်။ Syntax မှာ
tupleToSlice[Start index (included):Stop index (excluded):Increment]
: သည် slicing operator ဖြစ်သည်။
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits)
print(fruits[-1]) # return last element in the list
print(fruits[2:5]) # return elements from index 2 to 4 ( index 5 is not included )
print(fruits[:4]) # returns the elements from 0 to 3 ( prints first four elements )
print(fruits[2:]) # returns the elements from index 2 to end
print(fruits[-4:-1]) # return elements index -4 to -1( not included )
a =fruits[2:5]
print(type(a) )
# for loop နှင့် range() ကို သုံး၍ tuple တစ်ခုတည်ဆောက်ခြင်း
# Create a new tuple containing only the even numbers in the tuple
numbers = tuple([i for i in range(1,11)])
numbers
even = numbers[1::2]
even
# Create a new tuple containing only the odd numbers in the tuple
odd = numbers[::2]
odd
Temporary variable မသုံးဘဲ tuple အတွင်းရှိ item များ၏ value များကို ဖလှယ်(exchange) ကို tuple unpacking ဟုခေါ်သည်။
password = 'swordfish'
icecream = 'tuttifrutti'
password, icecream = icecream, password
password
icecream
The tuple() conversion function makes tuples from other things:
x = (0, 1, 2, 3, 4)
print(type(x)) # tuple ဖြစ်ကြောင်း စစ်ရန်
x = list(x) # 'tuple' မှ 'list' အဖြစ်သို့ ပြောင်းသည်။
print(type(x)) # list ဖြစ်ကြောင်း စစ်ရန်
list1 = [2, 4, 6]
y = tuple(list1)
print(y, type(y)) # 'tuple' အဖြစ်သို့ ပြောင်းသည်။
x = (1, 2, 3)
print(x)
x သည် tuple ဖြစ်သောကြောင့် delete လုပ်၍ မ၍ပါ။ Delete လုပ်လျှင် TypeError ပေးလိမ့်မည်။
del(x[1]) # TypeError
x သည် tuple ဖြစ်သောကြောင့် item assignment လုပ်၍ မ၍ပါ။ လုပ်လျှင် TypeError ပေးလိမ့်မည်။
x[1] = 8 # TypeError
y = ([1, 2], 3) # a tuple where the first item is a list
del(y[0][1]) # delete the 2
print(y) # the list within the tuple is mutable
# concatenating two tuples works
y += (4,)
print(y)
Tuple သည် immutable ဖြစ်သောကြောင့် index method နှင့် count method သာ ရှိသည်။ ထို Tuple method နှစ်ခုသည် list တွင်ရှိသည့် index method နှင့် count method အလုပ်လုပ်ပုံနှင့် လုံးဝတူညီသည်။
Python တွင် tuple နှင့် သက်ဆိုင်သည့် method အနည်းငယ်သာရှိသည်။ any(): method သည် tuple အတွင်းရှိ element များသည် iterable element များ ဟုတ် မဟုတ် စစ်ပေးသည်။
pets = ('cat', 'dog', 'horse')
any(pets)
count() method သည် tuple အတွင်း၌ သိလိုသည့် item များ အကြိမ်အရေအတွက်ကို ရေတွက်ပေးသည်။ တစ်နည်းအားဖြင့် သိလိုသည့် item တစ်ခုသည် tuple အတွင်း၌ အကြိမ် မည်မျှပါဝင်နေသည်ကို သိလိုသည့်အခါ အသုံးပြုသည်။ tuple.count(element)
pets = ('cat', 'dog', 'horse')
pets.count("cat")
min() method သည် tuple အတွင်း၌ အကြိမ်အရေအတွက် အနည်းဆုံးပါဝင်နေသည့် element ကို ဖော်ပြပေးသည်။
pets = ('cat', 'dog', 'horse')
min(pets)
max() method သည် tuple အတွင်း၌ အကြိမ်အရေအတွက် အများဆုံးပါဝင်နေသည့် element ကို ဖော်ပြပေးသည်။
pets = ('cat', 'dog', 'horse')
max(pets)
len() method သည် tuple အတွင်း၌ ပါဝင်နေသည့် element များ၏ အရေအတွက်(total number of elements) ကို ဖော်ပြပေးသည်။
pets = ('cat', 'dog', 'horse')
len(pets)
Tuple များသည် string ကဲ့သို့ ပေါင်းစပ်(concatenate)ခြင်း ပြုလုပ်နိုင်သည်။
pets = ('cat', 'dog', 'horse')
wild = ('lion', 'zebra', 'antelope')
animals = pets + wild
print(animals)
if statement နှင့် in ကို သုံးထားသည့် tuple ဥပမာ-
# Create a tuple named zoo that contains your favorite animals.
zoo = ("monkey", "liger", "quokka", "elephant")
# Determine if an animal is in your tuple by using value in tuple.
if "elephant" in zoo:
print("Yes, there is an elephant in your zoo.")
else:
print("No, but there should be")
Tuple တစ်ခုထဲတွင် တခြားသော tuple များ အထပ်လိုက် ထားနိုင်သည်။ Multidimensional tuple ဟုခေါ်သည်။ Multidimensional tuple သည် Multidimensional list လုံးဝသဘောတရားနှင့် အလုပ်လုပ်ပုံတူညီသည်။ Multidimensional list ကို list အခန်းတွင် ရှင်းပြခဲ့ပြီး ဖြစ်သည်။
Python list နှင့် tuple တို့၏ method အမျိုးမျိုးကို လက်တွေ့အသုံးချနိုင်သည်အဆင့်ထိ ကြိုးစားလေ့ကျင့်သင့်သည်။ list နှင့် tuple တို့ကို ကျွမ်းကျင်စွာ အသုံးပြုနိုင်လျှင် ပရိုဂရမ်ရေးသည့်အခါ ပိုမြန်ဆန် အဆင်ပြေပြီး အမှားနည်းလာလိမ့်မည်။ - End of Tuple Section -