Junior — Middle
Explain what a data structure that combines multiple elements into a single immutable sequence is.
sobes.tech AI
Answer from AI
In Python, a data structure that combines multiple elements into a single immutable sequence is called a tuple. Tuples are similar to lists, but unlike lists, they cannot be changed after creation — this ensures immutability.
Example of a tuple:
my_tuple = (1, 2, 3, 'a', 'b')
print(my_tuple[0]) # Will output 1
Tuples are useful when you need to guarantee that data will not be modified, for example, as keys in dictionaries or for passing fixed sets of values.