Write a Python program to get the 4th element and 4th element from last of a tuple


The program defines a tuple t containing 10 elements of different data types (strings and integers). Then the print() function is used to output the entire tuple.

Next, the 4th element of the tuple is accessed using indexing, with the expression t[3]. This value is then assigned to the variable i and printed to the console with the message "4th Elements From Tuple :".

Finally, the 4th element from the end of the tuple is accessed using negative indexing, with the expression t[-4]. This value is then assigned to the variable j and printed to the console with the message "4th Elements From Last Tuple :".

Source Code

t = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
print(t)
#Get item (4th element)of the tuple by index
i = t[3]
print("4th Elements From Tuple :",i)
#Get item (4th element from last)by index negative
j = t[-4]
print("4th Elements From Last Tuple :",j)

Output

('w', 3, 'r', 'e', 's', 'o', 'u', 'r', 'c', 'e')
4th Elements From Tuple : e
4th Elements From Last Tuple : u

Example Programs