What is the tuple in Python?
However, the difference between the two is that the tuple is immutable while lists are mutable.
That means, once created, a tuple cannot be changed while a list can be changed.
Besides, a tuple is enclosed in parenthesis () while lists, generally, are enclosed in square brackets [].
Another difference between the tuples and lists is that the tuples are heterogeneous data structures while lists are homogenous.
Syntax of constructing Python tuples
The general syntax of creating tuples:
The tuple items are comma separated and may store mixed items as well:
An example of creating a numbered tuple
In this example, a tuple of five numbers is created and displayed by using a for loop.
The code for creating and displaying tuple:
tup_ex = (10,20,30,40,50) for i in tup_ex: print ("The tuple item:",i)
Output:
An example of creating tuple of strings
A tuple containing string items is also separated by commas as shown in the example below:
This is how the tuple of string is created and displayed by using a for loop:
# An example of constructing a tuple of strings str_tuple = ('Tuple','is','a','compound','data type','in', 'Python') for tup_item in str_tuple: print ("The current tuple item:",tup_item)
Result:
An example of creating a tuple with mixed items
A tuple in Python may contain mixed items, for example, numbers and strings. See this example where a tuple is created with seven numbers and seven strings:
The code:
# An example of constructing a mixed item tuple mixed_tuple = (1,'List',2,'is',3,'a',4,'compound',5,'data type',6,'in', 7,'Python') for mixed_item in mixed_tuple: print ("The current mixed tuple item:",mixed_item)
Output:
Accessing specific elements of a tuple example
The specific tuple items can be accessed by using the index of elements. The element index starts at zero. Use the square brackets to enclose an index or indices as shown in the example below:
The code:
#Demo of accessing tuple elements by index numbers a_tuple = (1,'Tuples',2,'are',3,'heterogeneous',4,'data structures') print (a_tuple[1], a_tuple[3], a_tuple[5], a_tuple[7])
The above code output:
Creating a tuple from other tuples
To demonstrate that, two tuples are created with three elements in each.
The third tuple is created based on the first two tuples as shown in the example below:
The code:
#Demo of tuples tup1 = (1,2,3) tup2 = (10,20,30) tup3 = tup1 + tup2 print ("The third tuple elements:" ,tup3)
Output:
Getting length of a tuple demo
You may get the length of a tuple by using the len function.
Simply, provide a tuple name in the parenthesis after the len keyword, as shown in the example below:
The code for getting the length of a tuple:
# An example of a tuple tup_ex = (10,20,30,40,50) print ("Total number of elements in tuple = ", len(tup_ex))
Output:
Deleting tuple items?
As mentioned earlier, the tuples are immutable; once created, they cannot be changed.
So you cannot remove tuple items individually. However, by using the del method, you can remove a tuple entirely.
In that case, if you try accessing the deleted tuple, it will generate an error.
Creating a tuple from a list example
You may use the tuple() method to convert a list or sequence into a tuple.
- In this example, a list of three strings is created.
- After that, the tuple method is used to create a new tuple by converting the list elements:
The code:
# An example of a tuple lst = ['a','b','c'] tup_ex = tuple(lst) print ("New tuple elements after conversion = ", len(tup_ex))
Result:
Using max function for getting maximum value
See an example below where two tuples are created; one of the numbers and the other is strings.
After that, the max function is used to get maximum values in both tuples:
The code:
# An example of a tuple tup_numbers = (3,-5,100,500,50,2) tup_strings = ('The', 'Python', 'is', 'Powerful') print ("The maximum value in numbered tuple = ", max(tup_numbers)) print ("The maximum value in string tuple = ", max(tup_strings))
Output:
Getting minimum value
Similarly, you may use the min function to get minimum values in the given tuples.
The code for getting the minimum values:
# An example of a tuple tup_numbers = (3,-5,100,500,50,2) tup_strings = ('The', 'Python', 'is', 'Powerful') print ("The minimum value in numbered tuple = ", min(tup_numbers)) print ("The minimum value in string tuple = ", min(tup_strings))
Output: