People conversant with software programming would surely know the concept of lists or arrays wherein they can store multiple objects of similar or dissimilar data types into a congregated data structure; and then use absolute or relative referencing to access each member of the structure.
A tuple is pretty much in line to the concept of list with the key difference that it is immutable in nature – which means that the value of a tuple cannot be changed as per the need of the algorithm. Hence, tuple can be defined as a static list (or sequence).
The section below shows the various operations which can be done using tuples in Python; in terms of how to create tuples, access tuples, update/delete tuples and so on.
Creating tuples in Python
In Python, creating a tuple is extremely simple and syntactically convenient; quite similar to creating a collection.
Example – tuple1 = (‘Java’, ‘Ruby’, 2019, True)
The above syntax would create a tuple named ‘tuple1’ which can store a heterogeneous set of data in it (may not from the same datatype).
Furthermore, if the developer is not sure of the content of a tuple, he/she can create an empty tuple as well and initialize it later. Tuple2 = (); itself would create an empty tuple with the name ‘tuple2’.
Accessing elements of tuples in Python
A tuple in Python is accessed pretty much the same way as lists or arrays in other programming languages. For example – if a tuple say ‘tuple1’ contains elements such as ‘Good’, ‘Better’, ‘Best’; then tuple1 would return the first element of the tuple which is ‘Good’.
Now, the developer can also access more than 1 element of a tuple as well. The syntax for the same is tuple [1:2] which would return (‘Better’, ‘Best’).
Updating values in a tuple in Python
This is an interesting section of study as already mentioned in the previous sections, tuples cannot be edited or modified. Therefore, it is not technically possible to update values in a tuple; rather, portions of two or more tuples can be used to generate a third tuple based on the specific requirements.
Suppose – tuple 1 has elements 100, 200; and tuple 2 has elements ‘Bangalore’, and ‘Mumbai’ – then the content of the two tuples can be joined into a third tuple by simply adding the two tuples –
Tuple3 = tuple1 + tuple 2 – would combine the four elements into a single tuple.
Basic operations related to tuples in Python
Like the creation, access & update procedures mentioned before; there are other basic arithmetic and logical operations as well which can be done using tuple operators – a few of which are mentioned below for reference –
- Calculating the length of a tuple i.e. number of elements in a tuple – using ‘len’ operator
- Concatenation of two or more tuples can be done using ‘+’ operator
- Looping operation or iteration – FOR loop can be used to traverse through a tuple
Built-in implicit tuple functions in Python
A few of the built-in functions corresponding to tuples in Python are –
- cmp – compare two tuples and checks if they are same or not
- max – checks the maximum value in the tuple & returns the same
- min - checks the minimum value in the tuple & returns the same
- TUPLE – This is a type cast operator which is used to convert a list, array or sequence to a tuple.
Watch this video to get some practical knowledge on Tuples