A tuple is a sequence, similar to a list. The difference is that with a Tuple, it’s data is immutable. i.e. Once the data is set, it cannot change.
When you create a tuple, you are going to enclose it within parentheses () instead of square brackets.
my_tuple = (1,2,3,4,5)
print(my_tuple)
# prints (1,2,3,4,5)
Like with lists, they can contain numbers, strings, or other data types.
names = ("Jeff", "Christina", "Joe", "Leanne")
for name in names :
print(name)
# notice the singular use of name, to get the individual name in our tuple called names
Because tuples are immutable, they do not support methods like append, insert, remove, reverse, or sort, like a list does.
If you cannot change the values of a tuple, why bother having them. Well, it comes down to performance. Tuples can be processed faster, and with less resources (both CPU and RAM) because they are not weighed down with the chance of being changed.
Converting between Tuples and Lists
Using the Tuple() and List() functions, you can convert between the two.
num_tuple = (1,2,3)
num_list = list(num_tuple)
print(num_list)
name_list = ["Tom", "Sally", "Richard"]
name_tuple = tuple(name_list)
print(name_tuple)
Working with Tuples in Python was originally found on Access 2 Learn