Fundamentals of Python Programming
Everything You Need to Know About Tuples in Python | Data Science
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 with the concept of a 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, a tuple can be defined as a static list (or sequence).
How To Use Tuples
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.
1. 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 be 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’.
2. 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 says ‘tuple1’ contains elements such as ‘Good’, ‘Better’, ‘Best’; then tuple1[0] 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’).
3. Updating values in a Tuple in Python
This is an interesting section of the 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 + tuple2 – would combine the four elements into a single tuple.
4. 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 the ‘len’ operator
- Concatenation of two or more tuples can be done using the ‘+’ operator
- Looping operation or iteration – FOR loop can be used to traverse through a tuple
5. 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 the 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 typecast operator which is used to convert a list, array, or sequence to a tuple.
Watch this video to get some practical knowledge on Tuples
If you want to learn more about python, you can check out Board Infinity's python programming certification course, which will introduce you to fundamental programming concepts including data structures, networked application program interfaces, and databases. You will gain expert knowledge in Python irrespective of whether you took this course as a hobby or as a portfolio builder.