- We can access either by index or by slice operator
By using index
Python
t=(10,20,30,40,50,60)
print(t[0])
print(t[-1])
print(t[100])
Output
PowerShell
10
60
IndexError: tuple index out of range
By using slice operator
Python
t=(10,20,30,40,50,60)
print(t[2:5])
print(t[2:100])
print(t[::2])
Output
PowerShell
(30, 40, 50)
(30, 40, 50, 60)
(10, 30, 50)
Tuple vs immutability
- Once we creates tuple, we cannot change its content. Hence tuple objects are immutable.
Example
Python
t=(10,20,30,40)
t[1]=70
Output
Python
TypeError: 'tuple' object does not support item assignment
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!