Python Program to Get Tuple Items

Write a Python program to get or access tuple items with an example. In this programming language, we can access tuple items using the positive and negative index positions. In this Python example, we get or retrieve, or access the Python tuple items using both the positive and negative index position.

# Get Tuple Items

intTuple = (10, 20, 30, 40, 50, 60, 70, 80, 90)
print("Tuple Items = ", intTuple)

fourthItem = intTuple[3]
print("Tuple Fourth Item = ", fourthItem)

firstItem = intTuple[0]
print("Tuple First Item = ", firstItem)

fourthItemfromLast = intTuple[-4]
print("Tuple Fourth Item from Last = ", fourthItemfromLast)

sixthItemfromLast = intTuple[-6]
print("Tuple Sixth Item from Last  = ", sixthItemfromLast)
Python Program to Get Tuple Items

The program below shows how to get a range of tuple items by slicing a tuple with start and end index positions. Here, 1:5 means start slicing from index position 1 (2) and end at index position 4 (5)

n = (1, 2, 3, 4, 5, 6, 7, 8, 9)
print(n[1:5])
print(n[3:7])
(2, 3, 4, 5)
(4, 5, 6, 7)

This program uses a for loop to get tuple items one by one. This approach helps perform mathematical or string operations on individual tuple items.

n = (1, 2, 3, 4, 5)
for i in n:
print(i, end = ' ')
1 2 3 4 5 

Also Read