Python Tuple is a sequence data type. It is simply a collection of a number of comma-separated values. These values do not need to be of the same datatype. Python Tuple elements are enclosed in parenthesis while declaration. It is possible to nest a tuple within another tuple using parenthesis. A tuple within another tuple is considered as element of the outer tuple. Tuple is heterogeneous datatype and is immutable.
Syntax for Creating or Declaring Python Tuple
Tuple_name=(element1,element2, element3,….)
Tuple_name is the name of the tuple to be created
element1, element2, element3…. – are the elements of the tuple. These can be of any datatype even a tuple or a list.
Example
ex_tuple=(1234,’20 wellington street’,’London’,’Kelly lives here’)
The individual elements cannot be reassigned once created (immutable). ex_tuple[2]=’Venice’
The presence of a Python list as a tuple element can help you in defining a mutable tuple element.
An empty tuple can be created by assigning empty parenthesis to a tuple
Empty_tuple=()
A tuple of one element can be created by assigning single value to the tuple terminated with a comma.
Single_element_tuple=123456,
Comparing Python Tuple with Python List?
- Python Tuple is similar to Python list as far as the data type of elements is concerned. Both are hetergenous.
- Tuple is immutable whereas a List is mutable. You can update elements of a List but not that of a Tuple. A list added to tuple as its element is mutable.
- List elements are accessed by using indices of the elements . Tuple elements can also be accessed with name of tuple and indices in square parenthesis . Sequence unpacking of tuple elements is also used to access elements. This is done by placing comma separated variables on left of assignment operator followed by a pre-declared Python Tuple.
var1,var2,var3…=tuple_name
Number of variables on the left of assignment operator must be equal to the number of elements of the tuple already created.
Example
Ex_tuple is a four element tuple. For sequence unpacking of this tuple four variables v1,v2,v3, and v4 assigned the tuple elements’ values as in below example. These variables display all the elements of tuple when printed using Print function.
v1,v2,v3,v4=ex_tuple
Nesting of Python Tuple
A tuple can have another tuple as its element. For the outer tuple, inner tuple is an element. To access element of the inner tuple you can use indices in square bracket in sequence from outer to inner tuples.
nested_tuple=(‘James’,’Smith’,(12,’high grounds’,’London’),4000,’Sales and marketing’)
Python Tuple with a List as an Element
Python Tuple can have Lists as elements. To access element of the List you can use indices in square bracket in sequence from outer tuple to inner list.
Tuple are not immutable. Lists are mutable. Lists as tuple as element are mutable. You can update elements of the List elements within a Tuple.
tuple_with_list=(‘James’, ‘Smith’,[10,20,30])
Python tuple is a versatile heterogeneous sequence data type. It gives you flexibility of creating a collection of data values of different types under single name. Though immutable, you can make them mutable by adding list as elements.
Be First to Comment