Tuple concepts in python

By
What is Tuple:-
  • Tuple is usually a collection of heterogeneous objects enclosed within ()(small bracket).
  • A tuple is the collection of comma (,)separated values enclosed with small brackets.
  • The parentheses (small bracket) are optional in tuple
    .
e.g.
tuple1=(10,20,180,105)
tuple2=('Horse','Lion','Cow')
tuple3=("MNT LAB")
tuple4=("MNT LAB",)
print(type(tuple1))
print(type(tuple2))
print(type(tuple3))
print(type(tuple4))
Output:-
<class 'tuple'>
<class 'tuple'>
<class 'str'>
<class 'tuple'>
>>> 
Properties of Tuple:-
There are some properties of tuple as given below:-
  • An empty tuple can created as given below.
e.g.
Tuple=()
  • Creating a tuple with one item is slightly different .Here we will need to put comma (,) after the item as given below:-
e.g.
t1=(100)
t2=(100,)
t3=("MNT LAB")
t4=("MNT LAB",)
print(type(t1))
print(type(t2))
print(type(t3))
print(type(t4))
Output:-
<class 'int'>
<class 'tuple'>
<class 'str'>
<class 'tuple'>
>>> 
  • We can drop the parenthesis (small bracket ) in tuple, while initializing a tuple.
e.g.
#Two way to declared and initializing a tuple
t1=(10,20,30,40,50)
t2=10,20,30,40,50
t3=("MNT LAB",'Ramesh')
t4="MNT LAB",'Ramesh'
print(type(t1))
print(type(t2))
print(type(t3))
print(type(t4))
Output:-
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
>>> 
  • Tuples are immutable like string .Means we can not change the value after creating the tuple.
e.g.
#tuple are immutable
t1=(10,20,30,40,50)
print(t1)
t1[0]=100 #give error
t4[500]#give error
Output:-
(10, 20, 30, 40, 50)
Traceback (most recent call last):
  File "C:/Users/RAMASHANKER VERMA/Desktop/tuple.py", line 4, in <module>
    t1[0]=100 #give error
TypeError: 'tuple' object does not support item assignment
>>> 
  • Tuples are immutable,but they can contain mutable objects like list
e.g.
#tuple are immutable.but it can contain mutable objects.
t1=([10,20,30],[40,50],['ram','Neha'])
print(t1)
Output:-
([10, 20, 30], [40, 50], ['ram', 'Neha'])
>>> 
  • A tuple may contain duplicate items like list.
e.g.
#tuple may contain duplicate items.
t1=(10,20,30,20,10,30,50,70)
print(t1)
Output:-
(10, 20, 30, 20, 10, 30, 50, 70)
>>> 
  • Tuples are used for handling fixed length data where as lists are handling  variable length data.
  • Tuple provides less functionality than list.
Where can we use tuple:-
We can use tuple in following place as given below:-
  • When we want to clear data,we have to use tuple instead of list.Because tuple data is constant and must not be changed.
  • Tuple can be used as  a dictionary using nested structure.
e.g.
#tuple can be used as dictionary.
tuple1=((101,'Ram',30),(102,'Neha',20),(103,'Mohan',45))
print(tuple1
)
Output:-
((101, 'Ram', 30), (102, 'Neha', 20), (103, 'Mohan', 45))
>>> 

Accessing Tuple Elements:-
  • Like List and string ,tuple elements can be accessed using indices(index). 
  • We can access the tuple elements using indexing and slicing technique as given below:-
tuple=('a','e','i','o','u')
e.g.
Syntax:-
tuple(start:end:step)
start:-It denotes the starting index value of the tuple.
end:- It denotes the last index value of the tuple.
step:- It denotes the increment value of the ordered tuple.

Access the element in forward direction:- We can accessed the tuple elements  forward direction using positive(+) indexing techniques as given below:-
e.g.
tuple1=('a','e','i','o','u')
print(tuple1[0])
print(tuple1[1])
print(tuple1[2])
print(tuple1[3])
print(tuple1[4])
#slice the elements
print("slice element values:")
print(tuple1[:])
print(tuple1[1:4])
print(tuple1[1:])
print(tuple1[1:4:2])
print(tuple1[:4])
Output:-
a
e
i
o
u
slice element values:
('a', 'e', 'i', 'o', 'u')
('e', 'i', 'o')
('e', 'i', 'o', 'u')
('e', 'o')
('a', 'e', 'i', 'o')
>>> 
Access the element in backward direction:-
We can accessed the tuple elements  backward direction using negative(-) indexing techniques as given below:

e.g.
tuple2=['a','e','i','o','u']
print(tuple2[-1])
print(tuple2[-2])
print(tuple2[-3])
print(tuple2[-4])
print(tuple2[-5])
#slice the elements
print("slice element values:")
print(tuple2[:])
print(tuple2[:-1])
print(tuple2[-3:-1])
print(tuple2[-3:])
print(tuple2[-1:-4:-2])
print(tuple2[:-5])
Output:-
u
o
i
e
a
slice element values:
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o']
['i', 'o']
['i', 'o', 'u']
['u', 'i']
[]
>>> 
  • Entire tuple can be printed by just using the name of the tuple.
e.g.
tuple1=(20,40,'ram','a',40.30)
print(tuple1)
Output:-
(20, 40, 'ram', 'a', 40.3)
>>> 
  • Tuples can be iterated using a for loop like list.
e.g.
t1=(20,40,'ram','a',40.30)
print(t1)
count=0
for i in t1:
    print("t1[%d]=%s"%(count,i))
    count=count+1
Output:-
(20, 40, 'ram', 'a', 40.3)
t1[0]=20
t1[1]=40
t1[2]=ram
t1[3]=a
t1[4]=40.3
>>> 

e.g.

items=(
    (101,'Ram',30),
    (102,'Neha',20),
    (103,'Mohan',45)
    )
for a,b,c in items:
    print(a,b,c)
Output:-
101 Ram 30
102 Neha 20
103 Mohan 45
>>> 

Tuple Operations:
There are some operations of tuple as given below:-
  • Tuples are immutable.
e.g.
t1=('Mohan','Ram','Suresh')
print(t1)
t1[0]='Neha'#give error
t1[0:2]=('Raj','Pankaj','sandeep')
Output:-
('Mohan', 'Ram', 'Suresh')
Traceback (most recent call last):
  File "C:\Users\RAMASHANKER VERMA\Desktop\tuple.py", line 3, in <module>
    t1[0]='Neha'#give error
TypeError: 'tuple' object does not support item assignment
>>> 
  • Create a tuple.
e.g.
t1=('Mohan','Ram','Suresh',10,20)
  • We can concatenate the one tuple  to another tuple as given below.
e.g.
t1=(10,20,30,40,50)
t2=(60,70,80,90,100)
t=t1+t2
print(t)
Output:-
(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
>>> 
Deleting tuple:-
  • Tuple items can not be deleted by using del keyword.To delete the entire tuple or to delete the whole tuple at time,we can use the del keyword with tuple name.
  • Since tuple are immutable operations like oppend ,remove,insert, reverse, sort,del,,do not work with tuple.
e.g.
t=(10,20,30,40,50)
print(t)
#Now delete the complete tuple
del(t)
print(t) 
del(t[0]) #give error
del(t[1])#give error
print(t)
Output:-
(10, 20, 30, 40, 50)
Traceback (most recent call last):
  File "C:\Users\RAMASHANKER VERMA\Desktop\tuple.py", line 5, in <module>
    print(t)
NameError: name 't' is not defined
>>> 
  • Membership operator can be used with tuple.
e.g.
tuple1=(10,20,30,40,50,60,70)
print(20 in tuple1)
print(70 in tuple1)
print(80 in tuple1)
print(100 not in tuple1)
Output:-
True
True
False
True
>>> 
  • We can convert the string into tuple.
e.g.
str1="AMERICA"
a=tuple(str1)
print("Convert the string value to the tuple value:\n",a)
Output:-
Convert the string value to the tuple value:
 ('A', 'M', 'E', 'R', 'I', 'C', 'A')
>>> 
  • max():-It return maximum element in tuple.
e.g.
str1=('A','M','E','R','I','C','A')
str2=(1,2,3,4,5,6,7,8)
print("Maximum value of list is=",max(str1))
print("Maximum value of list is=",max(str2))
Output:-
Maximum value of list is= R
Maximum value of list is= 8
>>> 
  • min():-It returns maximum element in tuple.
e.g.
str1=('A','M','E','R','I','C','A')
str2=(1,2,3,4,5,6,7,8)
print("Minimum value of list is=",min(str1))
print("Minimum value of list is=",min(str2))
Output:-
Minimum value of list is= A
Minimum value of list is= 1
>>> 
  • Sorted():-It returns sorted element of the tuple and element in tuple remains unchanged.
e.g.
str1=('A','M','E','R','I','C','A')
str2=(70,50,60,40,20,80,30,40)
print("Sorted value of the tuple is=",sorted(str1))
print("Sorted value of the tuple is=",sorted(str2))
Output:-
Sorted value of the tuple is= ['A', 'A', 'C', 'E', 'I', 'M', 'R']
Sorted value of the tuple is= [20, 30, 40, 40, 50, 60, 70, 80]
>>> 
  • sum() :-It is used to return the sum of all the tuple values.
e.g.
str1=(70,50,60,40,20,80,10,40)
print("sum of all tuple value is=",sum(str1))
Output:-
sum of all tuple value is= 370
>>> 
  • Tuple.index():-It returns the index of the particular item of the tuple.
e.g.
str1=(70,50,60,40,20,80,10,40)
a=str1.index(10)
print("Index of the tuple element is:=",a)
Output:-
Index of the tuple element is:= 6
>>>
  • Tuple.count():-It returns number of times items occurs in tuple.
e.g.
str1=(20,50,60,40,20,80,40,40)
a=str1.count(40)
print("Number of times items are present:=",a)
Output:-
Number of times items are present:= 3
>>> 
Tuple Varieties:-
There are some varieties of tuple present in python as given below:-
  • It is possible to create a tuple of tuples.
e.g.
a=(1,3,5,7,9,11,13)
b=(2,4,6,8,10,12,14)
c=(a,b)
print(c[0][4],c[1][5])
#c[0][4]-> 0th tuple(first tuple) of 4th index
#c[1][5]-> 1th list(second tuple) of 5th index
Output:-
9 12
>>> 
  • A tuple may be embedded in other tuple.
e.g.
a=(1,3,5,7,9,11,13)
b=(2,a,4,6,8,10,12,14)
print("After embedded tuple is: ",b)
Output:-
After embedded tuple is:  (2, (1, 3, 5, 7, 9, 11, 13), 4, 6, 8, 10, 12, 14)
>>> 
  • It is possible to unpack a tuple within a list using the * operator.
e.g.
a=(1,3,5,7,9,11,13)
b=(2,4,6,*a,8,10,12)
print("After unpacked tuple is: ",b)
Output:-
After unpacked tuple is:  (2, 4, 6, 1, 3, 5, 7, 9, 11, 13, 8, 10, 12)
>>>

Program 1:How to add tuple element and print it.

t1=tuple(input("enter tuple elements\n"))
print(t1)
count=0
for i in t1:
    print("t1[%d]=%s"%(count,i))
    count=count+1
Output:-
enter tuple elements
123456789
('1', '2', '3', '4', '5', '6', '7', '8', '9')
t1[0]=1
t1[1]=2
t1[2]=3
t1[3]=4
t1[4]=5
t1[5]=6
t1[6]=7
t1[7]=8
t1[8]=9
>>> 
Watch complete lecture video
For More...

0 comments:

Post a Comment

Powered by Blogger.