How do I index tuples in Python with examples?

In Python, tuples are immutable sequences used to store collections of items. You can access individual elements in a tuple using their index. Indexing in Python starts at 0, meaning the first item in a tuple has an index of 0, the second item has an index of 1, and so on.

To access elements in a tuple, you can simply use the tuple name followed by the index enclosed in square brackets. Here is an example of how to index a tuple:

# Defining a tuple my_tuple = (10, 20, 30, 40, 50) # Accessing elements using indexing first_element = my_tuple[0] # 10 second_element = my_tuple[1] # 20 last_element = my_tuple[-1] # 50 (negative index for last element) print(first_element) # Output: 10 print(second_element) # Output: 20 print(last_element) # Output: 50

Python Tuples Indexing in Python Accessing Tuple Elements Python Programming