Python Tuples
In Python, a tuple is an immutable sequence type. This means that once you create a tuple, you can not change the values it contains: you can not add, remove, or modify the values of the elements in the tuple.
You can also use the len()
function to get the length of a tuple, and the in operator to check if an element is in a tuple.
Creating a tuple
Tuples are defined using parentheses, with the elements separated by commas.
This is an example of a tuple:
Accessing Values in tuples
We can access the values of a tuple by using the square bracket and the index as found in the example below:
Iterating through a tuple
A tuple is an iterable, this means that we can iterate through the values of a tuple using same pattern as we have seen in lists.
This is an example of how to iterate within a tuple
Returning tuples from function
Tuples are often used to return multiple values from a function, since they allow you to return multiple values in a single return statement.
Last updated