Python programming language, the tuple is similar to a list with the difference is that we cannot change the items of a tuple once it is assigned whereas, in the list, items can be modified. The Tuple is a collection of Python objects separated by commas.
A tuple is a collection which is ordered and unchangeable. Let us take a simple example of Tuple in Python.
deserts = ('oreo', 'cupcake', 'nougat', 'jellybean', 'pie')
tuples are immutable,but member object may be mutable.
A tuple is formed by placing all the elements inside the parentheses (), separated by a comma. The parentheses are optional but are a good practice to write it. A tuple can have any number of items, and they may be of different types. Let us see the following example of different types of items in one tuple.
deserts = ('oreo', 1, True, ['Android', 'iOS'], 'pie')
print(deserts)
In the above example, we have taken string, integer, list datatype to create a tuple and it is a valid tuple.
Let us write a tuple without parentheses.
deserts = 'oreo', 1, True, ['Android', 'iOS'], 'pie'
print(deserts)
Above code is also valid but, it does not best practice to write that is why we add the parentheses around the items.
Tuples can contain other compound objects, including lists, dictionaries, and other tuples. Hence, tuples can be nested inside of other tuples.
Listing 1 shows the beginning of a Python script that
Example:
tuple 1 =
(0, 1, 2, 3)
tuple 2 =
('python', 'geek')
tuple 3 =
(tuple 1, tuple 2)
print(tuple 3)
Output :
((0, 1, 2, 3), ('python', 'geek'))
A tuple is a sequence of values which can be of any type and they are indexed by integer.like lists,there could be positive indexing like 0,1,2,3,...... or negative indexing like -1,-2,-3,-4,......etc.
the tuple index has to be a