![]() |
VOOZH | about |
Tuples are immutable sequences in Python that can store a collection of items. Often, we might need to the retrieve the first and last elements from the tuple. In this article, we'll explore how to achieve this using the various methods in Python.
We can use indexing to access the first and last elements of the tuple. Python indexing starts at 0 for the first element and -1 for last element.
Output :
First Element: 10
Last Element: 50The Tuple unpacking allows to the assign the elements of a tuple to the individual variables. By unpacking we can easily get the first and last elements of a tuple:
Output :
First Element: 10
Last Element: 50We can also use slicing to get the first and last elements of a tuple. Slicing allows you to the specify a range of indices to the extract elements:
Output :
First Element: 10
Last Element: 50