Programming in Python
(Data Type: Tuple)
Dr. Faisal Anwer
Department of Computer Science
Aligarh Muslim University 1
Copyright © Dept of Computer Science, AMU, Aligarh. Permission required for reproduction or display.
Recap
• Creating List variable
• Accessing List
• List Slicing
• Operations & Functions of List
• Methods of List
Contents
• Creating Tuple variable
• Accessing Tuple
• Tuple Slicing
• Operations & Functions of Tuple
• Methods of Tuple
Python Tuple
A tuple is a sequence of Python objects separated by commas.
A tuple is similar to a list in terms of indexing, nested objects and
repetition but it is immutable unlike lists which are mutable.
Example
The tuple is created by enclosing elements in parentheses (Optional)
tup1 = (111, 12, 11)
tup2 = 111, 12, 11
An empty tuple can be created by empty parentheses:
tup4 = () or tup5 = tuple()
A nested tuple can be created by having the tuple inside the tuple :
tup6 = ((1,2), 3, 4, ‘John’, ‘Ahmad’, ‘Anil’, ‘Khan’)
Advantages of Tuple over List
If you have data that doesn't change, implementing it as tuple will
guarantee that it remains write-protected.
Tuples contain immutable elements and can be used as key for
a dictionary. With list, this is not possible.
Python Tuple: Accessing Elements
The tuple index can be either positive or negative
Positive index starts from left of tuple and it ranges from 0 to
(length of tuple) minus 1.
Negative index starts from right side of tuple and it ranges from -1
to minus length of tuple (i.e. –length ).
To access values in tuple, use the square bracket with the index of the
element.
Example-
tup1= ( ‘Rahil', ‘Amar', ‘Sahil’, (‘Amit’, Anil'), 1, 2, 3 )
Positive Index -tup1[0], tup1[1], tup1[2], tup1[3]
Negative Index-tup1[-4], tup1[-3], tup1[-2], tup1[-1]
Nested tuple-tup1[3][0], tup1[3][1]
Python Tuple: Range Function
The range function returns an object of range class that can be
converted to tuple using tuple() constructor.
Example-
print(tuple(range(4)) ------(0, 1, 2, 3)
print(tuple(range(1, 4)) ------(1, 2, 3)
print(tuple(range(2, 10, 2)) ------(2, 4, 6, 8)
print(tuple(range(-1, -11, -2)) ------(-1, -3, -5, -7, -9)
Tuple: Operations & Functions
Python-Sequences
Common Common
Operations Functions
Slicing Concatenate len() min() & max()
Membership Repeat Index() Count()
Tuple Slices
A slice is a tuple consisting of elements of a given tuple.
Slicing is similar to indexing except
use three index values high, low, and step_value
tup[low: high: step_value]
Default value is 0 for low, 1 for step_value and (total length
-1) for high.
Example: tup1 = ('a', 'b', 'c', 'd', 'e', 'f’)
tup1[1:3] ('b', 'c’)
tup1[:4] ('a', 'b', 'c', 'd’)
tup1[3:] ('d', 'e', 'f’)
tup1[:] ('a', 'b', 'c', 'd', 'e', ‘f’)
tup1[::2] ('a', 'c', ‘e’)
tup1[::-1] ('f', 'e', 'd', 'c', 'b', 'a’)
Tuple Operations
a = (1, 2, 3); b = (3, 4, 5, 6, 7 )
Concatenation (+)
Repetition (*)
Membership operators (in/not in)
The in operator returns true if a element exists in the given tuple.
The not in operator returns true if a element does not exists in the
given tuple.
Example: 1 in (3, 4, 5, 6, 7 ) returns True.
9 not in (3, 4, 5, 6, 7 ) returns False.
Tuple Functions
a = (1, 2, 3); b = (3, 4, 5, 6, 6, 7 )
len() – len(a)
It returns the length of the string.
Example: len(a) returns 3
min() & max() --- min(a) & max(a)
With a single iterable argument, return its smallest item.
With two or more arguments, return the smallest argument.
Example: min(a) returns 1
Tuple Methods
The syntax of a method call is:
Object_name.method_name(arg1, arg2, ….)
The methods may also expect arguments and return values.
The methods of tuple are as follows-
count()
index()
Tuple is immutable/mutable
Strings are "immutable" while Lists are "mutable“.
Python tuples have a surprising trait:
They are immutable, but the values in mutable object may be
changed.
This may happen when a tuple holds a reference to any mutable
object, such as a list.
Example:
A = (‘Amit', 1, [2, 3])
A[0][0] = 'B’ Error
A[2].append(5)
A (‘Amit', 1, [2, 3, 5])
Accessing Elements of Tuple
friends = ('Joseph', 'Glenn', 'Sally')
for friend in friends:
print('Happy New Year:', friend)
for i in range(len(friends)) :
friend = friends[i]
print('Happy New Year:', friend)
Deleting a Tuple
Delete Tuples-To explicitly remove an entire tuple, just use the
del statement.
Example −
tuple1 = (‘Maths’, ‘India', 1407)
del(tuple1)
Clone of Tuple
import copy as c
#create a tuple
tup = ("Python", 50, 60, [], True, 97)
print('The tuple is', tup)
print('The identity of tuple is', id(tup))
#make a clone of a tuple using deepcopy() function
tup_clone = c.deepcopy(tup)
tup_clone[3].append(230)
print('The clone of tuple is', tup_clone)
print('The identity of clone of tuple is', id(tup_clone))
#make a reference of tuple
tup_ref = tup[:]
print('The tuple is', tup_ref)
print('The identity of tuple is', id(tup_ref))
Exercises
• Write a Python program to find the repeated items of a
tuple.
• Write a Python program to find the index of an item in a
tuple.
• Write a Python program to remove an empty tuple(s)
from a list of tuples.
Summary
• Creating Tuple variable
• Accessing Tuple
• Tuple Slicing
• Operations & Functions of Tuple
• Methods of Tuple