Tuple
Tuple
tup1
Tuple 1 2 3 4 5
Tuple – What is it?!
▪ A tuple is a sequence of immutable (that cannot be changed) Python objects.
▪ Tuples are sequences, just like lists.
▪ The main difference between the tuples and the lists is that
▪ the tuples cannot be changed
▪ unlike lists. Tuples use parentheses, whereas lists use square brackets.
Example:
Memory
tup1 = ('physics', 'chemistry', 1997)
tup2 = (1, 2, 3, 4, 5 )
tup2 1 2 3 4 5
tup3 = "a", "b", "c", "d"
Tuple – Declare
Output
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
('physics', 'chemistry', 1997, 2000)
tup3 = "a", "b", "c", "d"
(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd')
print(tup1)
print(tup2)
print(tup3)
tup_empty = ()
Empty tuple: ()
print("Empty tuple: ", tup_empty)
To write a tuple containing a single value you have to include a comma, even though there is only
one value.
(50,)
tup4 = (50,)
print(tup4)
Tuple – Indexing index or indices
▪ To carry out different useful operations on the list, e.g., sorting the marks, find min/max
marks or price
▪ Immutable data, meaning accidental change / modification are not possible, e.g.,
coordinates of a location.
▪ Tuples are immutable so; It doesn't require extra space to store new objects.
▪ It also explains the slight difference in indexing speed is faster than lists, because in
tuples for indexing it follows fewer pointers.
Tuple – Update
Tuples are immutable, which means you cannot update or change the values of tuple elements.
You can take portions of the existing tuples to create new tuples.
Output
Output???
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
Traceback (most recent call last):
# delete entire tuple is allowed File "c:\Users\M SYEED\OneDrive - American International
del(tup2) University-Bangladesh\Desktop\Lab19\tuple.py", line 8, in
<module>
# delete single item is not allowed del(tup1[0])
del(tup1[0])
TypeError: 'tuple' object doesn't support item deletion
print('After deleting..')
print(tup1)
Tuple – Basic Operations
Tuple – Basic Operations
tup = ('physics', 'chemistry', 1997, 2000, 'Raining!', 1976)
print ('Total items in tuple: ', len(tup))
strToTuple=tuple(str)
print ("After Conversion : ", strToTuple)
Tuple
Exercise
Check Your understanding
Output???
tup = ('apple', 75, 4.89, 'Abdullah', 'Dhaka')
tuple1 =tuple()
tuple1 = tup
tuple1[1] = 'pineapple'
print('tuple1: ', tuple1)
tup = ('physics', 2001, 'chemistry', 1997, 'Rainy day!', 1976)
print (len(tup))
intuple = 'programming' in tup
print('Is programming in the tuple: ', intuple)
print(tup)
tup2 = (1,2)
tup = tup + tup2
print(tup)
tup2 = tup2*3
print(tup2)
2. Create a tuple with name cities and insert 5 name of the cities.
3. Create a tuple with name mixed and insert following values: apple, 75, 4.89, Abdullah, Dhaka
5. Find the minimum, maximum and avg from the tuple numbers, and print them.
6. Get the summation of the numbers from tuple mixed and print the summation result.
8. Merge the two tuples numbers and cities together and print values of the new list.
9. How many items are there in the new tuple? Show the total number of items.