Tuples

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 အသုံးပြုရသည် အဓိက အားသာချက်များမှာ

  • မတူညီသည့် ဒေတာ အမျိုးအစားများ(different (heterogeneous) data types)အတွက် သင့်လျော်သည်။
  • Tuple များသည် immutable ဖြစ်ခြင်းကြောင့် dictionary ၏ key (key for a dictionary)အဖြစ် အသုံးပြုနိုင်သည်။ Dictionaries များအကြောင်းကို နောက်ပိုင်းအခန်းများတွင် ရှင်းပြထားသည်။
  • Tuple များကို Iterating လုပ်ခြင်းသည် list များကို iterating လုပ်ခြင်းထက် ပို၍ မြန်ဆန်သည်။
  • ပြင်ရန် ပြောင်းရန် မလိုသည့် data များ passing လုပ်သည့်အခါ tuple များ ပိုကောင်းသည်။
  • Tuple များသည် နေရာ အနည်းငယ်သာယူသည်။
  • မှား၍ ဖျက်မိခြင်း ပြောင်းမိခြင်း မဖြစ်နိုင်ပါ။ (can’t clobber tuple items by mistake.)
  • Tuple များကို dictionary keys အဖြစ် အသုံးပြုနိုင်သည်။
  • Tuple များကို function argument အဖြစ်သုံး၍ function ထဲသို့ ထည့်ပေးနိုင်သည်။

Tuple အသစ် ပြုလုပ်ခြင်း (Constructing Tuples)

Tuple အသစ်တစ်ခု တည်ဆောက်နိုင်သည့် နည်းများ(creating new tuples)
(၁) လက်သည်းကွင်း () ကို အသုံးပြု၍ ကော်မာခံရေး၍လည်း tuple ပြုလုပ်နိုင်သည်။
(၂) tuple() ကို အသုံးပြု၍ tuple ပြုလုပ်နိုင်သည်။

In [1]:
# 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))
(2,) <class 'tuple'>
In [2]:
empty_tuple = () # Empty tuple အသစ် တစ်ခု တည်ဆောက်သည်။ 
print(empty_tuple)
print(type(empty_tuple)) # type() ဖြင့် အမျိုးအစားကို စစ်ဆေးသည်။ 
()
<class 'tuple'>
In [3]:
x = (0,1,2,3,4,5,6,7,8,9) # Integer tuple အသစ် တစ်ခု တည်ဆောက်သည်။ 
x
Out[3]:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
In [4]:
pet1  = [ 'dog', 'cat', 'parrot']
print(type(pet1))
pet2 = tuple(pet1)  # ဖြင့် ရှိပြီးသား list တစ်ခုကို  tuple တစ်ခု အဖြစ်သို့ပြောင်းသည်။ 
print(type(pet2))
<class 'list'>
<class 'tuple'>

Tuple နှင့် list ထဲတွင် ဘာမျှမရှိသည့် empty tuple နှင့် empty list တို့ ဖြစ်နိုင်သည်။

Element တစ်ခုထက်ပိုများသည့် tuple ပြုလုပ်ရန်အတွက် လက်သည်းကွင်း ()ထဲတွင် element များကို ကော်မာခံ၍ ထည့်ရေးသည်။

In [5]:
pets = ('dog', 'cat', 'parrot')
pets
Out[5]:
('dog', 'cat', 'parrot')
In [6]:
# လက်သည်းကွင်း ()မပါဘဲ  element ၏ နောက်တွင် ကော်မာ ထည့်ရေး၍လည်း tuple ပြုလုပ်နိုင်သည်။
pets = 'dog',
pets
Out[6]:
('dog',)
In [7]:
x = 2,    # the comma tells Python it's a tuple
print(x, type(x)) 
(2,) <class 'tuple'>
In [8]:
# Element တစ်ခုထက်ပိုများပါက  
# လက်သည်းကွင်း ()မပါဘဲ ကော်မာ(comma) ခံရေး၍လည်း  tuple ပြုလုပ်နိုင်သည်။
pets = 'dog', 'cat', 'parrot'
pets
Out[8]:
('dog', 'cat', 'parrot')

လက်သည်းကွင်း (parentheses) မပါဘဲ နောက်ဆုံး element ၏ နောက်တွင် ကော်မာ(comma) ထည့်ရေး၍လည်း tuple ပြုလုပ်နိုင်သည်။ သို့သော် လက်သည်းကွင်း (parentheses) ထဲတွင် ထည့်ရေးခြင်းကြောင့် ဖတ်ရလွယ်သည်။ ပရိုရမ်က output ထုတ်ပေးသည့်အခါ နားလည်မှု မရှုတ်ထွေးစေရန် လက်သည်းကွင်း (parentheses) ထဲတွင် ထည့်၍ အမြဲဖော်ပြလေ့ရှိသည်။

Tuples as records

Tuple များကို မှတ်တမ်း မှတ်ရာများကို သိမ်းဆည်းရာအဖြစ် အသုံးပြုနိုင်သည်။ (Tuples as records)

In [9]:
#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) 

Tuple unpacking

  • ကို သုံး၍ ပိုသည့် excess item များကို စုထည့်ခြင်း
    *args ဖြင့်သည့် function parameter ကို အသုံးပြုသည်။ range(5) သည် [0, 1, 2, 3, 4] ဖြစ်သည် ။ အောက်တွင် a ကို range မှ ပထမ item ၊ b ကို ဒုတိယ item အဖြစ် သတ်မှတ်ပြီး ကျန်သည့် ကိန်းများအားလုံးအတွက် * ကို သုံး၍ တစ်စုတည်ပေါင်းပြီး item တစ်ခု အဖြစ်သတ်မှတ်သည်။
In [10]:
x = ['pig', 'cow', 'horse']
a, b, c = x
print(a, b, c)
pig cow horse
In [11]:
a, b, *rest = range(5)  
a, b, rest
Out[11]:
(0, 1, [2, 3, 4])
In [12]:
a, b, *rest = range(3)
a, b, rest
Out[12]:
(0, 1, [2])
In [13]:
a, b, *rest = range(2)
a, b, rest
Out[13]:
(0, 1, [])
In [14]:
# * prefix
a, *body, c, d = range(5)#  Index နံပါတ် 1, 3 နှင့် 4 ကို သက်ဆိုင်ရာ တို့ဖြင့် assign လုပ်ပြီး ကျန်ကိန်းများအားလုံးအတွက် *body
a, body, c, d
Out[14]:
(0, [1, 2], 3, 4)
In [15]:
*head, b, c, d = range(5)
head, b, c, d
Out[15]:
([0, 1], 2, 3, 4)

Constructors - list အသစ်ပြုလုပ်ခြင်း

In [16]:
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)
[0, 1, 2, 3, 4, 5, 6, 7]
[25, 36, 49, 64, 81]

Nested Tuple

ပရိုဂရမ်က output တွင် လက်သည်းကွင်း (parentheses) ထဲတွင် ထည့်၍ အမြဲဖော်ပြရသည့်တခြားသော အကြောင်းမှာ nested tuple များကို ရှင်းလင်းထင်ရှားစွာ ဖတ်နိုင်ရန် ဖြစ်သည်။

In [17]:
pets = ('dog', 'cat'), 'parrot'
pets
Out[17]:
(('dog', 'cat'), 'parrot')

('dog', 'cat') သည် main tuple ထဲတွင် ပါဝင်နေသည့် subtuple တစ်ခုဖြစ်သည်။ nested tuple ထဲတွင် tuple ပေါင်းများစွာ အကန့်အသတ်မရှိ ထည့်ထားနိုင်သည်။ သတိပြုရန်အချက်မှာ main tuple ထဲတွင် tuple အလွှာပေါင်းများစွာ ထည့်ထားခြင်းကြောင့် ဖတ်ရန် နှင့် tuple တည်ဆောက်ထားပုံ(structure)ကို နားလည်ရန် ခက်ခဲ့လိမ့်မည်။

Integer Vs Tuple with one element

x = 2 ဟုသိထားသည့်အခါ ထို x ၏ data type ကိုလည်း သတိပြုရန်လိုသည်။ အောက်က ဥပမာတွင် x သည် integer တစ်ခု ဖြစ်နိုင်သလို tuple တစ်ခုလည်းဖြစ်နိုင်သည်။ သို့သော် tuple ဖြစ်ကြောင်းထင်ရှားစေရန် လက်သည်းကွင်း() ထဲတွင် ဖော်ပြထားသည်။

In [18]:
x =(2)
print(x)
print(type(x))
2
<class 'int'>
In [19]:
x = (2,)
print(x)
print(type(x))
(2,)
<class 'tuple'>

Tuple တွင် variable များစွာကို တပြိုက်နက် assign လုပ်နိုင်သည်။

In [20]:
vehicle = ('Toyota', 'BMW', 'Benz')
a, b, c = vehicle 
a
Out[20]:
'Toyota'
In [21]:
a, b, c
Out[21]:
('Toyota', 'BMW', 'Benz')

Accessing Tuple Elements

Tuple အတွင်းရှိ element များကို နည်း နှစ်မျိုးဖြင့် access လုပ်နိုင်သည်။

  • Indexing (element တစ်ခုကို access လုပ်လိုလျှင်)
  • Slicing ( တစ်ခုထက်ပိုများသည့် element များကို access လုပ်လိုလျှင်)
In [22]:
# Accessing Tuple Elements Using Indexing
pets = 'dog', 'cat', 'parrot'
pets[1]  # to access the second index:
Out[22]:
'cat'

မရှိသည့် element ကို access လုပ်လျှင် IndexError: tuple index out of range ပေးလိမ့်မည်။

In [24]:
pets[3] # IndexError: tuple index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-24-3bdb4175e66b> in <module>
----> 1 pets[3] # IndexError: tuple index out of range

IndexError: tuple index out of range

အနောက်မှ စတင်၍ ပြန်လည် ညွှန်ပြလိုလျှင် negative index များကို အသုံးပြုသည်။ -1 သည် နောက်ဆုံးအလုံး(last element in the tuple) ဖြစ်သည်။ -2 သည် ဒုတိယနောက်ဆုံးအလုံး(second from last element in the tuple ဖြစ်သည်။

In [25]:
pets = 'dog', 'cat', 'parrot', 'gerbil'
pets[-1]
Out[25]:
'gerbil'
In [26]:
pets[-2]
Out[26]:
'parrot'

Tuple ၏ index နံပါတ်များသည် (list indices) ကိန်းပြည့်များ(integers)သာ ဖြစ်ရမည်။ ကိန်းပြည့်များ(integers) ခဲ့လျှင် TypeError ပေးလိမ့်မည်။

In [27]:
pets = 'dog', 'cat', 'parrot'
pets['1']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-dffb97419613> in <module>
      1 pets = 'dog', 'cat', 'parrot'
----> 2 pets['1']

TypeError: tuple indices must be integers or slices, not str

Tuple သည် immutable ဖြစ်သော်လည်း tuple ထဲတွင် ရှိနေသည့် list သည် mutable ဖြစ်သည်။ (tuples are immutable, but member objects may be mutable.)

In [28]:
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)
(1, 2, 3)
([1], 3)
([1], 3, 4)

Slicing

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]

  • stop position ကို သုံးထားသောကြောင့် slice လုပ်မည့် အရေအတွက်(length of a slice)ကို အလွယ်တကူသိနိုင်သည်။ (easy to see the length of a slice or range when only the stop position is given)။ ဥပမာ range(3) နှင့် my_list[:3] တို့တွင် item (၃)ခုပါရမည့်ဟု မြင်ရုံနှင့် တန်းသိနိုင်သည်။
  • slice တစ်ခု သို့မဟုတ် range တစ်ခုတွင် ပါဝင်နေမည့် အရေအတွက်ကို start နေရာနှင့် stop နေရာကို ကြည့်၍ အလွယ်တကူတွက်နိုင်သည်။ (item အရေအတွက် = အစနေရာ - အဆုံး နေရာ)
  • sequence တစ်ခုကို အပိုင်း (၂)ခု အဖြစ် အလွယ်တကူ ခွဲထုတ်ပစ်နိုင်သည်။
In [29]:
l = [10, 20, 30, 40, 50, 60]
l[:2] # split at 2
Out[29]:
[10, 20]
In [30]:
l[2:]
Out[30]:
[30, 40, 50, 60]
In [31]:
l[:3] # split at 3
Out[31]:
[10, 20, 30]
In [32]:
l[3:]
Out[32]:
[40, 50, 60]

Slice objects

Start index ၊Stop index ၊ Increment တို့တန်ဖိုးများကို ဘာမှ မထည့်ပေးခြင်းသည် item အားလုံးကို ရည်ညွန်းသည်။

In [33]:
s =  tuple(range (0,10))
s[ : : ]
Out[33]:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
In [34]:
# Item (၃)ခု မြောက် item ကိုယူရန်
s =  tuple(range (0,10))
s[::3]
Out[34]:
(0, 3, 6, 9)
In [35]:
# နောက်မှ စ,သည်။ ပြောင်းပြန် ပြန်စီသည်။ 
s[::-1]
Out[35]:
(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
In [36]:
#  နောက်မှ စ,၍ ပြောင်းပြန် ပြန်စီသည်။ (၂)ခု မြောက် item ကိုယူရန် ။ 
s[::-2]
Out[36]:
(9, 7, 5, 3, 1)

Accessing Tuple Elements Using Slicing

indexing သည် element တစ်ခုချင်းစီ (individual element) ကိုသာ access လုပ်နိုင်သည်။ တစ်လုံးထက်ပိုများသည့် element များ(subset များ)ကို access လုပ်လိုသည်အခါ slicing ကို အသုံးပြုရသည်။ Syntax မှာ

tupleToSlice[Start index (included):Stop index (excluded):Increment]

: သည် slicing operator ဖြစ်သည်။

  • Start index:
    The index at which to start the slicing. The element at this index is included in the slice. If this parameter is absent, it is assumed to be zero, and thus, the slicing starts at the beginning of the tuple.
  • Stop index:
    The index at which to stop slicing. The element at this index is not included in the slice. This means that the last item in this slice will be the one just before the stop index. If this parameter is absent, the slice ends at the very end of the tuple.
  • Increment:
    This determines how many steps to take in the tuple when creating the slice. If this parameter is absent, it is assumed to be one.
    Fruits အမည် ဖြင့် tuple တစ်ခုတည်ဆောက်ပြီး လုပ်ပုံကို ဖော်ပြထားသည်။
In [37]:
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) )
('apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango')
mango
('cherry', 'orange', 'kiwi')
('apple', 'banana', 'cherry', 'orange')
('cherry', 'orange', 'kiwi', 'melon', 'mango')
('orange', 'kiwi', 'melon')
<class 'tuple'>
In [38]:
# 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 
Out[38]:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
In [39]:
even = numbers[1::2]
even 
Out[39]:
(2, 4, 6, 8, 10)
In [40]:
# Create a new tuple containing only the odd numbers in the tuple
odd = numbers[::2] 
odd
Out[40]:
(1, 3, 5, 7, 9)

Tuple Unpacking

Temporary variable မသုံးဘဲ tuple အတွင်းရှိ item များ၏ value များကို ဖလှယ်(exchange) ကို tuple unpacking ဟုခေါ်သည်။

In [41]:
password = 'swordfish'
icecream = 'tuttifrutti'
password, icecream = icecream, password
password
Out[41]:
'tuttifrutti'
In [42]:
icecream
Out[42]:
'swordfish'

The tuple() conversion function makes tuples from other things:

List ကို tuple ကို အဖြစ်သို့ပြောင်းနိုင်သည်။ List နှင့် tuple တို့သည့် data structure များ ဖြစ်ကြသည်။

In [43]:
x = (0, 1, 2, 3, 4)
print(type(x)) # tuple  ဖြစ်ကြောင်း စစ်ရန်

x = list(x)  #  'tuple' မှ 'list' အဖြစ်သို့ ပြောင်းသည်။
print(type(x))  # list  ဖြစ်ကြောင်း စစ်ရန်
<class 'tuple'>
<class 'list'>
In [44]:
list1 = [2, 4, 6]
y = tuple(list1) 
print(y, type(y)) #  'tuple' အဖြစ်သို့ ပြောင်းသည်။
(2, 4, 6) <class 'tuple'>
In [45]:
x = (1, 2, 3)
print(x)
(1, 2, 3)

TypeError

x သည် tuple ဖြစ်သောကြောင့် delete လုပ်၍ မ၍ပါ။ Delete လုပ်လျှင် TypeError ပေးလိမ့်မည်။

In [46]:
del(x[1])       # TypeError 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-028e97e8ebeb> in <module>
----> 1 del(x[1])       # TypeError

TypeError: 'tuple' object doesn't support item deletion

x သည် tuple ဖြစ်သောကြောင့် item assignment လုပ်၍ မ၍ပါ။ လုပ်လျှင် TypeError ပေးလိမ့်မည်။

In [47]:
x[1] = 8        # TypeError 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-fbf16785fc32> in <module>
----> 1 x[1] = 8        # TypeError

TypeError: 'tuple' object does not support item assignment
In [48]:
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
([1], 3)
In [49]:
# concatenating two tuples works
y += (4,)         
print(y)
([1], 3, 4)

Tuple Functions

Tuple သည် immutable ဖြစ်သောကြောင့် index method နှင့် count method သာ ရှိသည်။ ထို Tuple method နှစ်ခုသည် list တွင်ရှိသည့် index method နှင့် count method အလုပ်လုပ်ပုံနှင့် လုံးဝတူညီသည်။

Tuple Methods

Python တွင် tuple နှင့် သက်ဆိုင်သည့် method အနည်းငယ်သာရှိသည်။ any(): method သည် tuple အတွင်းရှိ element များသည် iterable element များ ဟုတ် မဟုတ် စစ်ပေးသည်။

In [50]:
pets = ('cat', 'dog', 'horse')
any(pets)
Out[50]:
True

count() method သည် tuple အတွင်း၌ သိလိုသည့် item များ အကြိမ်အရေအတွက်ကို ရေတွက်ပေးသည်။ တစ်နည်းအားဖြင့် သိလိုသည့် item တစ်ခုသည် tuple အတွင်း၌ အကြိမ် မည်မျှပါဝင်နေသည်ကို သိလိုသည့်အခါ အသုံးပြုသည်။ tuple.count(element)

In [51]:
pets = ('cat', 'dog', 'horse')
pets.count("cat")
Out[51]:
1

min() method သည် tuple အတွင်း၌ အကြိမ်အရေအတွက် အနည်းဆုံးပါဝင်နေသည့် element ကို ဖော်ပြပေးသည်။

In [52]:
pets = ('cat', 'dog', 'horse') 
min(pets)
Out[52]:
'cat'

max() method သည် tuple အတွင်း၌ အကြိမ်အရေအတွက် အများဆုံးပါဝင်နေသည့် element ကို ဖော်ပြပေးသည်။

In [53]:
pets = ('cat', 'dog', 'horse') 
max(pets)
Out[53]:
'horse'

len() method သည် tuple အတွင်း၌ ပါဝင်နေသည့် element များ၏ အရေအတွက်(total number of elements) ကို ဖော်ပြပေးသည်။

In [54]:
pets = ('cat', 'dog', 'horse')
len(pets)
Out[54]:
3

Tuple များသည် string ကဲ့သို့ ပေါင်းစပ်(concatenate)ခြင်း ပြုလုပ်နိုင်သည်။

In [55]:
pets = ('cat', 'dog', 'horse')
wild = ('lion', 'zebra', 'antelope')
animals = pets + wild
print(animals)
('cat', 'dog', 'horse', 'lion', 'zebra', 'antelope')

if statement နှင့် in ကို သုံးထားသည့် tuple ဥပမာ-

In [56]:
# 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")
Yes, there is an elephant in your zoo.

Multidimensional tuples

Tuple တစ်ခုထဲတွင် တခြားသော tuple များ အထပ်လိုက် ထားနိုင်သည်။ Multidimensional tuple ဟုခေါ်သည်။ Multidimensional tuple သည် Multidimensional list လုံးဝသဘောတရားနှင့် အလုပ်လုပ်ပုံတူညီသည်။ Multidimensional list ကို list အခန်းတွင် ရှင်းပြခဲ့ပြီး ဖြစ်သည်။

Python list နှင့် tuple တို့၏ method အမျိုးမျိုးကို လက်တွေ့အသုံးချနိုင်သည်အဆင့်ထိ ကြိုးစားလေ့ကျင့်သင့်သည်။ list နှင့် tuple တို့ကို ကျွမ်းကျင်စွာ အသုံးပြုနိုင်လျှင် ပရိုဂရမ်ရေးသည့်အခါ ပိုမြန်ဆန် အဆင်ပြေပြီး အမှားနည်းလာလိမ့်မည်။ - End of Tuple Section -