Python Tuples
Python Tuple is used to store the sequence of immutable python objects. Tuple is similar to lists since the value of the items stored in the list can be changed whereas the tuple is immutable and the value of the items stored in the tuple can not be changed. A tuple can be written as the collection of comma-separated values enclosed with the small brackets. A tuple can be defined as follows.
Example
Output: (10, 20, 30, 40, 50, 60) tuple1[0] = 10 tuple1[0] = 20 tuple1[0] = 30 tuple1[0] = 40 tuple1[0] = 50 tuple1[0] = 60 Example 2
Output: Enter the tuple elements ...12345 ('1', '2', '3', '4', '5') tuple1[0] = 1 tuple1[0] = 2 tuple1[0] = 3 tuple1[0] = 4 tuple1[0] = 5 However, if we try to reassign the items of a tuple, we would get an error as the tuple object doesn't support the item assignment. An empty tuple can be written as follows.
The tuple having a single value must include a comma as given below.
A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by using their specific index value. We will see all these aspects of tuple in this section of the tutorial. Tuple indexing and splittingThe indexing and slicing in tuple are similar to lists. The indexing in the tuple starts from 0 and goes to length(tuple) - 1. The items in the tuple can be accessed by using the slice operator. Python also allows us to use the colon operator to access multiple items in the tuple. Consider the following image to understand the indexing and slicing in detail. Unlike lists, the tuple items can not be deleted by using the del keyword as tuples are immutable. To delete an entire tuple, we can use the del keyword with the tuple name. Consider the following example.
Output: (1, 2, 3, 4, 5, 6) Traceback (most recent call last): File "tuple.py", line 4, in <module> print(tuple1) NameError: name 'tuple1' is not defined Like lists, the tuple elements can be accessed in both the directions. The right most element (last) of the tuple can be accessed by using the index -1. The elements from left to right are traversed using the negative indexing. Consider the following example.
Output: 5 2 Basic Tuple operationsThe operators like concatenation (+), repetition (*), Membership (in) works in the same way as they work with the list. Consider the following table for more detail. Let's say Tuple t = (1, 2, 3, 4, 5) and Tuple t1 = (6, 7, 8, 9) are declared.
Python Tuple inbuilt functions
Where use tupleUsing tuple instead of list is used in the following scenario. 1. Using tuple instead of list gives us a clear idea that tuple data is constant and must not be changed. 2. Tuple can simulate dictionary without keys. Consider the following nested structure which can be used as a dictionary.
3. Tuple can be used as the key inside dictionary due to its immutable nature. List VS Tuple
Nesting List and tupleWe can store list inside tuple or tuple inside the list up to any number of level. Lets see an example of how can we store the tuple inside the list.
Output: ----Printing list---- (101, 'Ayush', 22) (102, 'john', 29) (103, 'james', 45) (104, 'Ben', 34) ----Printing list after modification---- (110, 'David', 22) (102, 'john', 29) (103, 'james', 45) (104, 'Ben', 34) |