Tuples in Python
Learning objective
• Python Tuple
• Accessing Values in Tuples
• Updating Tuples
• Deleting Tuple Elements
• Basic Tuples Operations
• Built-in Tuple Functions
• Tuple Methods
• Difference Between List and Tuple
Tuple - Introduction
• A tuple is a sequence of immutable Python objects.
• Tuples are sequences, just like lists.
• Tuples cannot be changed unlike lists and tuples use parentheses
• A single tuple may contain Data Types like Integers, Strings, Objects.
• Creating a tuple is as simple as putting different CSVs.
• Optionally you can put these CSVs between parentheses.
tup1 = ('physics', 'chemistry', 1997, 2000);
Accessing values in Tuples
• To access values in tuple, use the square brackets for slicing along
with the index or indices to obtain value available at that index.
a = (1, 12.5, ‘a’, “python”, “Programming”)
print(a[1]) # Output is 12.5
print(a[3]) # Output is python
print(a[1:4]) # Output is [12.5, ’a’, ‘python’]
Accessing values in Tuples
T = (9,18,'Hi',12,"Hello",15.55,'Programming',100,125.5)
print(T[5])
print(T[1:5])
print(T[2:8])
print(T[2:9:3])
print(T[-1])
print(T[-5])
How to take every nth-element of a tuple?
What if we want to have only every 2-nd element of T? This is where the step
parameter comes into play.
Accessing values in Tuples
T = (9,18,'Hi',12,"Hello",15.55,'Programming',100,125.5)
print(T[0:9:3]) # Here ‘3’ is step parameter
# Now you also write the above code as
print(T[::3])
# Here both print(T[0:9:3]) and print(T[::3]) gives an output as (9, 12,
'Programming‘)
Accessing values in Tuples
T = (9,18,'Hi',12,"Hello",15.55,'Programming',100,125.5)
print (T[2::2]) # ['Hi', 'Hello', 'Programming', 125.5]
print (T[2::3]) # ['Hi', 15.55, 125.5]
print (T[3::3]) #[12, 'Programming']
print(T[5:-2]) # [15.55, 'Programming']
print(T[-2:-5:-1]) # [100, 'Programming', 15.55]
print(T[-2:-7:-2]) # [100, 15.55, 12]
Updating Tuples
• Tuples are immutable, we cannot update or change the values of tuple
elements.
• You are able to take portions of existing tuples to create new tuples.
tup1 = (10, 4.6)
tup2 = (‘Hi', ‘Hello')
# Following action is not valid for tuples
# tup1[0] = 100
# So let's create a new tuple as follows
tup3 = tup1 + tup2
print (tup3)
Deleting Tuple Elements
• Removing individual tuple elements is not possible.
• But you can delete the entire tuple.
• To explicitly remove an entire tuple, just use the del statement.
tup = ('Math', 'English', 85, 65)
print (tup)
del tup
Basic Tuple Operations
• Tuples respond to the + and * operators much like strings; they
mean concatenation and repetition here too, except that the
result is a new tuple, not a string.
Built-in Tuple Functions
• Python includes the following tuple functions:
• cmp(list1, list2) : Compares elements of both lists.
• Please note cmp() is not supported in python 3.
• len(tuple): Gives the total length of the list.
• max(tuple):Returns item from the list with max value.
• min(tuple): Returns item from the list with min value.
Built-in Tuple Functions
tuple1 = ('Python', 100, 'Programming', 2, 'is')
print(len(tuple1))
tuple2 = [2,5,8,7,10,20,1]
print(min(tuple2))
print(max(tuple2))
tuple3 =("arif","vijay","said","nasser")
print(len(tuple3))
print(min(tuple3))
print(max(tuple3))
Tuple Methods
• Python has ONLY two built-in methods that you can use on tuples.
• count(): Returns the number of times a specified value appers in a
tuple
• index(): Searches the tuple for a specified value and returns the
index of the first occurrence of a specified value in the tuple.
• Unlike lists, tuples are immutable, so they don’t have methods for
modifying their contents, like append, remove, or sort.
count() and index() methods
tuple1 = (10, 20, 30, 20, 10, 20)
# Using the count() method
count_of_20 = tuple1.count(20)
print("Count of 20:", count_of_20)
# Using the index() method
index_of_30 = tuple1.index(30)
print("Index of 30:", index_of_30)
Difference between List and Tuple
You must have learnt:
• Python Tuple
• Accessing Values in Tuples
• Updating Tuples
• Deleting Tuple Elements
• Basic Tuples Operations
• Built-in Tuple Functions
• Tuple Methods
• Difference Between List and Tuple