Tuples in python

  • Store multiple item in single varibale.
  • Built-in data type.
  • Collection which is ordered and unchangeable.
  • Tuples are written in round bracket.
girl=("ani","leni")
print(girl)
('ani', 'leni')

Tuple are :

  • Ordered - in a order
  • Unchnagebale - cant add or remove item after tuple is created.
  • Duplicate - since tuple are indexed, it can have two same item.

Tuple Length

len( ) function determine how mnay item a tuple consist.

girl=("ani","leni")
print(len(girl))
2

Creating tuple with one item

To create tuple with one item, we have to add ',' after item name, else it is not recognized by python.

girl=("ani",)
print(girl)
('ani',)

Data types used as tuple

Tuple can be of any data type.

girl=("ani",7,True)
print(girl)
('ani', 7, True)

type()

It is defined as objects with data type tuple.

<class 'tuple'>

girl=("ani",)
print(type(girl))
<class 'tuple'>

tuple() Constructor

It is possible to use tuple( ) constructor to make tuple.

girl=tuple(("ani","leni"))
print(girl)
# note: We have to use 2 brackets
('ani', 'leni')

Accessing Tuple

We can access tuple by its index number.

girl=("ani","leni")
print(girl[1])
leni
  • Negative Indexing
    -1 refers to last item, -2 refers to second last item and so on.
  • Range Indexing
girl=("ani","leni")
print(girl[-1])
leni
  • Check if item exists
    It uses in keyword.
girl=("ani","leni")
if "ani" in girl:
    print("yes")
yes

Updating Tuple

We can't change , add, remove tuple though there are some other works that can be done.

  • Change tuple value
    • convert tuple into list
    • change the list
    • convert list back to tuple
girl=("ani","leny")
chn = list(girl)
chn[1]="seni"
girl = tuple(chn)
print(girl)
('ani', 'seni')
  • Adding item in tuple
    • convert tuple into list
    • add item with use of .append
    • convert list back to tuple
girl=("ani","leny")
chn = list(girl)
chn.append("seni")
girl = tuple(chn)
print(girl)
('ani', 'leny', 'seni')
  • Adding tuple to tuple
girl=("ani","leny")
chn =("seni",)
adde= girl + chn
print(adde)
('ani', 'leny', 'seni')
  • Remove item in tuple
    Use of .remove instead of .append
girl=("ani","leny")
chn = list(girl)
chn.remove("leny")
girl = tuple(chn)
print(girl)
('ani',)
  • Delete tuple completely
    It shows error
girl=("ani","leny")
del girl
print(girl)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_6168\836222327.py in <module>
      1 girl=("ani","leny")
      2 del girl
----> 3 print(girl)

NameError: name 'girl' is not defined

Unpack tuple

Exracting value back to variable is called a unpacking of tuple.

girl=("ani","leny")
(lovely,cute)=girl
print(lovely)
print(cute)
ani
leny

Loop tuple

  • Use of for loop
girl=("ani","seni")
for x in girl:
    print(x)
ani
seni
  • Use of while loop
girl=("ani","seni")
i = 0
while i < len(girl):
    print(girl[i])
    i = i + 1
ani
seni

join tuple

We use '+' operator to join two or more tuples.

girl=("ani","sha")
age = (1,9)
tot = girl + age
print(tot)
('ani', 'sha', 1, 9)

Multiply tuple

We use '*' operator to multiply two or more tuples.

girl=("ani","sha")
new = girl * 2
print(new)
('ani', 'sha', 'ani', 'sha')

Tuple Method

  • count( ) : returns number of time specified value occurs in tuple.
  • index( ) : search list in tuple by its index number