Python Tuples
Python Tuples
In python, tuple is a collection of an ordered sequence of items to store the values of different data types. The tuple is the same as List, but the only
difference is the Tuples are immutable that means once you create the tuple object, you are not allowed to make any changes to the tuple object.
In python, you can create the tuple by enclosing the items within parentheses ( )And the tuple items must be separated by commas.
Following is the example of creating the tuple with a different type of items in python.
When you execute the above python tuple program, you will get the result as shown below.
Following is the example of accessing the elements from the tuple object in python.
When you execute the above python tuple example, you will get the result as shown below.
10
30
Tutlane
In python, you can also use negative indexing to access tuple object elements starting from the end. The index value -1 will refer to the last item, -
2 refers to the second-last item, and so on.
Following is the example of accessing the elements from the tuple object using negative indexing in python.
When you try to execute the above python tuple example, you will get an index out of range exception like as shown below.
The following is the example of accessing the specified range of elements from a tuple using a range of indexes in python.
If you observe the above result, the tuple items slicing started from the index position 1 and stopped one item before the end index position 4.
It’s optional to define the starting and ending index positions in python to get the range of values from a tuple. If you skip mentioning the starting
index position, the range will start from the first item the same way if you skip mentioning the ending index position, the range will continue to the
end of the tuple.
Following is the example of getting the range of tuple items with or without specifying the starting and ending index of tuple items.
('tutlane', 'learn')
(30, 'tutlane')
('learn', 'python')
tpl = ("tutlane",)
print(type(tpl))
tpl1 = ("tutlane")
print(type(tpl1))
The above python tuple example will return the result as shown below.
<class 'tuple'>
<class 'str'>
Following is the example of converting the tuple into a list, change list items, and convert the list to a tuple in python.
Following is the example of using del keyword to delete the complete tuple object in python.
Following is the example of using len() function in python to get the tuple length/size.
tpl = ()
icount = len(tpl)
if icount > 0:
print("Tuple size: ", icount)
else:
print("Tuple is empty")
The above tuple example will return the result as shown below.
Tuple is empty
Following is the example to verify whether the particular exists in the tuple or not using in operator in python.
20 in tuple: True
tutlane in tuple: True
50 in tuple: False
Following is the example of looping through the tuple object items using for loop in python.
The above python tuple example will return the result as shown below.
Method Description
count() It is useful to get the number of times the specified value occurs in the tuple.
index() It is useful to get the index position of the first occurrence of the specified value.
2
0