Tuples are immutable collection of objects. As a result, tuple elements cannot be modified, deleted or inserted. Just like Lists, Tuples can also be used to represent a collection which may have heterogeneous values. Tuples are typically used to represent objects, which make sense as a whole, when all elements are together.
For example, in a class, if you have to represent the complete details of a student who scored the highest marks in the class, you could represent the student record as a tuple as shown below:
top_student = ('jane', 'doe', 99, 21, 'f')
This tuple contains values for the student's first name, last name, total score, age and gender. This collection together make a record for one student even though each element is of different type. Since the values, though heterogeneous, represent one student with the highest score, you make a tuple representing the record.
Operation on tuples are similar to lists except, the modification operations are not allowed. Here is the run-down
|Positive positional index||top_student||'jane'||first position in the tuple|
|Negative positional index||top_student[-1]||'f'||first position from the end.|
|Slicing||top_student[1:3]||('doe', 99)||new tuple with numbers from position 1 (included) to 3 (excluded)|
|Slicing with start position default||top_student[:2]||('jane', 'doe')||new tuple with numbers from from 0 index and 2nd position (excluded)|
|Slicing with end position default||top_student[2:]||(99, 21, 'f')||new tuple with numbers from position 2 to end of the tuple|
|Build in function len||len(top_student)||5||len can be applied to tuples also to obtain the size of the tuple|
Range type represents an immutable sequence of numbers mostly used in for loops to help iterate a block of code a specific number of times.
start and step are optional.
Sequence starts from this value. 0 is the default when not supplied
The sequence of numbers are generated by incrementing the step value, starting from the start value.
The sequence of numbers are generated up to the stop value but not including the stop value
Here are a few examples:
|list(range(5))||[0,1,2,3,4]||Total number is 5 but the last index number is 4|
|list(range(1, 5))||[1,2,3,4]||Instead of starting at default 0, it starts at 1 since start number is given|
|list(range(0, 20, 5))||[0,5,10,15]||Since step is given, start should be given. Steps in increment of 5 - step value|
list(range(0, -5, -1))
|[0,-1,-2,-3,-4]||Same rule applies for negative numbers as well|
Points to note
- Tuples are very popular in Data Analytics and should be understood thoroughly.
- Tuples are more efficient than lists. When ever the elements do not change, you should use tuples.
- Lists are not as popular for Data Analytics as NumPy Arrays or Pandas Series. NumPy and Pandas are Python libraries heavily used in Data Analytics. We use NumPy Array or Pandas Series in place of Python lists, as these data structures are more efficient for handling large datasets and also provide many convenient functions to use in analytics.