python-tuto-4
November 23, 2024
Tuple
1. Tuple is similar to List except that the objects in tuple are immutable which means we cannot
change the elements of a tuple once assigned.
2. When we do not want to change the data over time, tuple is a preferred data type.
3. Iterating over the elements of a tuple is faster compared to iterating over a list.
Tuple Creation
[2]: tup1 = () # Empty tuple
[3]: tup2 = (10,30,60) # tuple of integers numbers
[4]: tup3 = (10.77,30.66,60.89) # tuple of float numbers
[5]: tup4 = ('one','two' , "three") # tuple of strings
[6]: tup5 = ('Asif', 25 ,(50, 100),(150, 90)) # Nested tuples
[52]: len(tup5)
[52]: 4
[54]: tup5[2][1]
[54]: 100
[7]: tup6 = (100, 'Asif', 17.765) # Tuple of mixed data types
[8]: tup7 = ('Asif', 25 ,[50, 100],[150, 90] , {'John' , 'David'} , (99,22,33))
[9]: len(tup7) #Length of list
[9]: 6
Tuple Indexing
[55]: tup2
1
[55]: (10, 30, 60)
[10]: tup2[0] # Retreive first element of the tuple
[10]: 10
[56]: tup4
[56]: ('one', 'two', 'three')
[11]: tup4[0] # Retreive first element of the tuple
[11]: 'one'
[12]: tup4[0][0] # Nested indexing - Access the first character of the first tuple␣
↪element
[12]: 'o'
[13]: tup4[-1] # Last item of the tuple
[13]: 'three'
[57]: tup5
[57]: ('Asif', 25, (50, 100), (150, 90))
[14]: tup5[-1] # Last item of the tuple
[14]: (150, 90)
Tuple Slicing
[15]: mytuple = ('one' , 'two' , 'three' , 'four' , 'five' , 'six' , 'seven' ,␣
↪'eight')
[16]: mytuple[0:3] # Return all items from 0th to 3rd index location
[16]: ('one', 'two', 'three')
[17]: mytuple[2:5] # List all items from 2nd to 5th index location
[17]: ('three', 'four', 'five')
[18]: mytuple[:3] # Return first three items
[18]: ('one', 'two', 'three')
2
[19]: mytuple[:2] # Return first two items
[19]: ('one', 'two')
[20]: mytuple[-3:] # Return last three items
[20]: ('six', 'seven', 'eight')
[21]: mytuple[-2:] # Return last two items
[21]: ('seven', 'eight')
[22]: mytuple[-1] # Return last item of the tuple
[22]: 'eight'
[23]: mytuple[:] # Return whole tuple
[23]: ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight')
Tuple update and change item
[24]: mytuple
[24]: ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight')
[25]: del mytuple[0] # Tuples are immutable which means we can't DELETE tuple items
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[25], line 1
----> 1 del mytuple[0]
TypeError: 'tuple' object doesn't support item deletion
[26]: mytuple[0] = 1 # Tuples are immutable which means we can't CHANGE tuple items
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[26], line 1
----> 1 mytuple[0] = 1
TypeError: 'tuple' object does not support item assignment
[27]: del mytuple # Deleting entire tuple object is possible
3
Loop Through Tuple
[30]: mytuple = ('one' , 'two' , 'three' , 'four' , 'five' , 'six' , 'seven' ,␣
↪'eight')
[31]: mytuple
[31]: ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight')
[59]: for i in mytuple:
print(i)
one
two
three
four
five
six
seven
eight
[34]: for i in enumerate(mytuple):
print(i)
(0, 'one')
(1, 'two')
(2, 'three')
(3, 'four')
(4, 'five')
(5, 'six')
(6, 'seven')
(7, 'eight')
Count
[35]: mytuple1 =('one', 'two', 'three', 'four', 'one', 'one', 'two', 'three')
[36]: mytuple1.count('one') # Number of times item "one" occurred in the tuple.
[36]: 3
[37]: mytuple1.count('two') # Occurence of item 'two' in the tuple
[37]: 2
[38]: mytuple1.count('four') #Occurence of item 'four' in the tuple
[38]: 1
4
Tuple Membership
[39]: mytuple
[39]: ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight')
[60]: 'one' in mytuple # Check if 'one' exist in the tuple
[60]: True
[41]: 'ten' in mytuple # Check if 'ten' exist in the tuple
[41]: False
[61]: if 'three' in mytuple: # Check if 'three' exist in the list
print('Three is present inm the tuple')
else:
print('Three is not present in the tuple')
Three is present inm the tuple
[43]: if 'eleven' in mytuple: # Check if 'eleven' exist in the list
print('eleven is present in the tuple')
else:
print('eleven is not present in the tuple')
eleven is not present in the tuple
Index Position
[44]: mytuple
[44]: ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight')
[45]: mytuple.index('one') # Index of first element equal to 'one'
[45]: 0
[46]: mytuple.index('five') # Index of first element equal to 'five'
[46]: 4
[47]: mytuple1
[47]: ('one', 'two', 'three', 'four', 'one', 'one', 'two', 'three')
[48]: mytuple1.index('one') # Index of first element equal to 'one'
[48]: 0
5
Sorting
[49]: mytuple2 = (43,67,99,12,6,90,67)
[50]: sorted(mytuple2) # Returns a new sorted list and doesn't change original tuple
[50]: [6, 12, 43, 67, 67, 90, 99]
[51]: sorted(mytuple2, reverse=True) # Sort in descending order
[51]: [99, 90, 67, 67, 43, 12, 6]
[ ]: