Write a Python program to get the 5th element and 5th element from last of a Tuple
Answers
Answered by
2
Required Answer:-
Question:
- Write a Python program to get 5th element and 5th element from the last of a tuple.
Solution:
Let us consider that the given tuple is,
a=(1,2,3,4,5,6,7,8,9,10,11)
To get the 5th element from first, write this:
- print("Fifth element from first:",a[4])
To get the 5th element from last, write this:
- print("Fifth element from last:",a[-5])
Explanation:
Index number of an array starts with zero.
Consider the array,
a = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 )
Index: 0 1 2 3 4 5 6 7 8 9 10
So, the index value of the fifth element is 4. Hence, to get the fifth element, we wrote a[4]
Again, Python also supports negative indexing.
Consider the array.
a = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 )
Index: -11, -10, -9, -8, -7, -6, -5, -4 -3 -2 -1
So, to get the fifth element from last, we wrote a[-5]
Refer to the attachment ☑.
Attachments:
Similar questions