How do I index tuples in Python with standard library only?

In Python, you can index tuples just like you do with lists, using square brackets. Tuples are immutable sequences of items, which means once they are created, their elements cannot be changed. Indexing allows you to access individual elements or slices of a tuple.

Here's how you can index a tuple:

# Creating a tuple my_tuple = (1, 2, 3, 4, 5) # Accessing elements by index first_element = my_tuple[0] # Returns 1 second_element = my_tuple[1] # Returns 2 # Accessing a slice of the tuple slice_of_tuple = my_tuple[1:3] # Returns (2, 3)

Python tuples indexing programming standard library