Junior — Middle
How is access to tuple elements performed using syntax in the programming language?
sobes.tech AI
Answer from AI
In Python, access to tuple elements is done by index using square brackets []. Indexing starts at zero.
Example:
my_tuple = (10, 20, 30, 40)
print(my_tuple[0]) # Outputs 10
print(my_tuple[2]) # Outputs 30
Negative indices can also be used to access elements from the end:
print(my_tuple[-1]) # Outputs 40
print(my_tuple[-3]) # Outputs 20
It is important to remember that tuples are immutable, so elements cannot be changed by index.