A tuple is a sequence data type that is similar to list. Tuples are immutable lists. It contains a set of numbers or characters or a combination of both separated by commas and enclosed within parenthesis (()). Lists are enclosed in square brackets () whereas tuple is within parenthesis (()).
The one thing that separates tuples from lists is that elements and size of a list can be changed whereas tuple cannot be updated. Tuples can be considered as read-only lists.
tuple1 = ('python', 45, 56, 12, 'learning') tuple2 = ('code', 3, 4, 7, 'program') print(tuple1) print(tuple1) print(tuple2[2:4]) print(tuple1[2:]) print(tuple1*2) print(tuple1+tuple2)
('python', 45, 56, 12, 'learning') python (4, 7) (56, 12, 'learning') ('python', 45, 56, 12, 'learning', 'python', 45, 56, 12, 'learning') ('python', 45, 56, 12, 'learning', 'code', 3, 4, 7, 'program')
In the above example, if you try to change an element of the tuple1, for e.g., tuple1 = 100, it would show an error message because elements and size of tuples cannot be changed. Whereas, in the case of lists, you can add as many new elements as you want.
There are various operations that can be done on tuple.
a) You can print out any element of the tuple by just mentioning the index of the element. Tuple indices start with 0. All you have to do is, enter the index of the element in square brackets followed by the variable referring the tuple and it will print out the element.
b) You can print out a particular part of the tuple just by mentioning the starting index and the ending index in square brackets separated by a colon (:) followed by the variable referring tuple.
c) If you want to print out a particular part of tuple starting from an index and going until the end if the tuple, then you don’t have to mention the ending index in square brackets. Leave that part blank and the interpreter will automatically understand that you want to print the tuple till the end from a particular index.
Same goes for the case where you want to print the part starting from the beginning and going to a particular index. All you have to do is leave the starting index part blank in square brackets and mention the ending index.
d) Tuples can be multiplied by a number. Here, by multiplying, we mean that the tuple will print out itself as many times as the number by which it is multiplied.
e) You can add two tuples. Which you add two or more tuples, the elements of the tuples get arranged in a single tuple.
REMEMBER: Adding tuples doesn’t mean that the elements of the tuples will get added and output will be the addition of the elements of the two tuples. No, it just combines the elements in a single tuple.
So why do we need tuples?
Tuple is not used frequently as lists in practice, but their immutability is the whole point. If you use lists to enter a collection of elements, it can be changed anywhere; if you use a tuple, it cannot. That is, tuples provide a sort of integrity constraint that is convenient for larger programs.