You are on page 1of 20

Chapter 7 : Tuples

By
Pankaj Singh - PGT (CS)
KV Bharatpur

Computer Science – Class XI


Tuples

• A tuple is a standard datatype of Python that can store a


sequence of values belonging to any type.
• These are immutable sequences of Python i.e. you cannot
change elements of a tuple in place.
• These are depicted through parenthesis i.e. round
brackets, e.g. following are some tuples in Python
• ()
• (1,2,3,4)
• (‘a’,’b’,’c’)
• (“alok”,”isha”,”bhavna”,”gaurav“)

Computer Science – Class XI


Creating Tuples

• Creating a tuple is similar to list creation, but we need to


put a number of expression in parenthesis.
• T=() # empty tuple

• T=(1,2,3,4,5) # tuple of integer values

• T=(23.5, 45, “class 11”) # mixed tuple

• T=(1,2,(2,1),5,3) # nested tuple

Computer Science – Class XI


Accessing Tuples

• The tuples are very much similar to list. The elements are
accessed by their index number from 0 to len-1 in forward
direction and from -1, -2, … to –len in backward direction.
>>> t=(1,2,3,4)
>>> t[0] # will give 1
>>> t[3] # will give 4
>>> t[-1] # will also give 4
• len(tuple-name) is used to find the length of tuple

Computer Science – Class XI


Traversing a Tuples

• Traversal a sequence means accessing and processing each


element of it.
>>> s="Demo"
>>> t=tuple(s)
>>> t
('D', 'e', 'm', 'o')
>>> for i in range (len(t)):
print (t[i])

D
e
m
o

Computer Science – Class XI


Traversing a Tuples

• Another Example:
>>> t=("Hello","this","is","a","pen")
>>> t # ('Hello', 'this', 'is', 'a', 'pen')
>>> for i in range (0, len(t)):
print ("Index ->",i,"Element ->",t[i])

Index -> 0 Element -> Hello


Index -> 1 Element -> this
Index -> 2 Element -> is
Index -> 3 Element -> a
Index -> 4 Element -> pen

Computer Science – Class XI


Tuples Operation

• Joining tuples
>>> t1=(1,2,3)
>>> t2=(11,2,34,2)
>>> t3=t1+t2
>>> t3
(1, 2, 3, 11, 2, 34, 2)

Computer Science – Class XI


Tuples Operation

• The + operator when used with tuples requires that both


the operands must be of tuple type. You cannot add a
number or any other value to a tuple. The following will
produce an ERROR:
• tuple + number eg: tpl1 + 2
• tuple + complex number eg: tpl1 + (2+3j)
• tuple + string eg: tpl1 + “HELLO”
• tuple + list eg: tpl1 + [34,55,33]

Computer Science – Class XI


Repeating or Replicating Tuples

• Like string and lists, we can use ‘*’ operator to replicate a


tuple specified number of times. eg:
>>> t1=(1,2,3)
>>> t1*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> t1
(1, 2, 3)
>>> t1*=3
>>> t1
(1, 2, 3, 1, 2, 3, 1, 2, 3)

Computer Science – Class XI


Slicing the Tuples

• Tuple slices, like list-slice or string slices are the sub parts of
the tuple extracted out. We can use indexes of tuple elements
to create tuple slice as per following format:
seq = tuple [start : end]
seq = tuple [start : end : step]
t1 = (12,51,45,14,15,47,16,95,47,15,25)
>>> s1=t1[3:-4]
>>> s1 # will print (14, 15, 47, 16)
>>> s2=t1[1:len(t1):2]
>>> s2 # will print (51, 14, 47, 95, 15)

Computer Science – Class XI


Comparing tuples

• We can compare two tuples without having to write code with loops for
it. We can use comparison operator for it
>>> t1=(2,3)
>>> t2=(2,3)
>>> t3=('2','3')
>>> t4=(5,2)
>>> t5=(2,3,4)
>>> t1==t2 # True
>>> t1==t3 # False
>>> t4<t2 # False
>>> t4>t5 # True
>>> t4==t5 # False

Computer Science – Class XI


Unpacking/packing tuples

• Creating a tuple from a set of values is called packing and


its reverse, i.e. creating individual values from a tuple’s
elements is called unpacking.
<variable1>,<var 2>,<var 3> = tuple

• Tuples unpacking requires that the list of variables on the


left has the same number of elements as the length of the
tuple.

Computer Science – Class XI


Unpacking/packing tuples

• Example:
>>> t1=(21,34,22)
>>> x,y,z = t1
>>> print (x)
21
>>> print (y)
34
>>> print (z)
22

Computer Science – Class XI


Tuples functions / methods

1. len () : it returns the length of the tuple.


t=(10,12,34,14)
le=len(t) # will return 4
2. max () : it returns the maximum value from the tuple.
mx=max(t) # will return 34
3. min () : it returns the minimum value from the tuple.
mi=min(t) # will return 10

Computer Science – Class XI


Tuples functions / methods

4. Index () : returns index of an existing element of a tuple.


t=(10,12,34,14)
ind=t.index(34) # will return 2
5. count () : returns the count of an element of a tuple.
ctr=t.count(34) # will return 1
6. tuple () : this method can be used to create tuple from
different types of values.

Computer Science – Class XI


Tuples functions / methods

6.1 creating emtpy tuple


t=tuple()
6.2 creating tuple from a list
t=tuple([1,2,3])
print (t) # t=(1,2,3)
6.3 creating tuple from a string
t=tuple(‘Hello’) # t=(‘H’,’e’,’l’,’l’,’o’)
6.4 creating a tuple from keys of a dictionary
t=tuple({1: ’one’, 2:’two’}) # t=(1,2)

Computer Science – Class XI


Indirectly modifying tuples

1. Using tuple unpacking : tuples are immutables. To


change the value, we would need to first unpack it,
change the value, and then again repack it. Example:
>>> t1=(10,20,30)
>>> t1 # (10, 20, 30)
>>> x,y,z=t1
>>> y=1500
>>> t1=(x,y,z)
>>> t1 # (10, 1500, 30)

Computer Science – Class XI


Indirectly modifying tuples

2. Using the constructor function of lists and tuple i.e. list()


and tuple()
>>> t=("swatantra","alok","divyansh")
>>> t # ('swatantra', 'alok', 'divyansh')
>>> L=list(t)
>>> L # ['swatantra', 'alok', 'divyansh']
>>> L[1]="Aditya"
>>> t=tuple(L)
>>> t # ('swatantra', 'Aditya', 'divyansh')

Computer Science – Class XI


Some important questions . . .

1. How are tuples different from a list when both are


sequences?
2. Create the following tuple using a for loop
1. A tuple containg sequence of integers from 1 to 50.
2. The tuple (‘a’, ‘bb’, ‘ccc’, . . . . ) that ends with 26 copies of
the letter ‘z’.
3. Given a tuple pair ((2,5),(4,2),(9,8),(12,10)), count the
number of pairs (a,b) such that both a and b are even.
4. What is the length of the tuple shown below:
t=((((‘a’,1),’b’,’c’),‘d’,2),‘e’,3)

Computer Science – Class XI


Thank you for your time!

20

You might also like