What is a Tuple in Python ?
A tuple is just like a List but the main difference between tuple and list is that Tuple is immutable means it can not be changed once you created it compared to Lists which you can change. A tuple is an immutable List. Another difference is that to represent tuple in python we use parenthesis compared to list which uses Square Brackets.
How to Create a Tuple in Python?
To create a tuple we use parenthesis and put every elements inside this parenthesis separated by commas.
In : # create an empty tuple In : empty_tup = () In : type(empty_tup) Out: tuple In : # create a tuple with integers In : int_tup = (1, 2, 3, 4, 5) In : type(int_tup) Out: tuple In : # a tuple with mix data type In : mix_tup = (1, 'hello', 2.4,[5, 6, 7]) In : type(mix_tup) Out: tuple In :
To create a tuple with only one element, you have to add a comma after the element.
In : not_tup = (5) In : type(not_tup) Out: int In : good_tup = (5,) In : type(good_tup) Out: tuple In :
Accessing Elements of a Tuple –
There are various way to access elements of a tuple. Let’s look at them one by one.
1 . Indexing –
To access elements from a tuple we use the square bracket followed by the index or position of the item in the tuple. Indexing in python always starts with 0 not 1. For example if you want to get the first item of a tuple you have to write tuple not tuple.
In : companies = ('Google','Apple','Microsoft','Facebook') In : # access for element In : companies Out: 'Google' In : # access the second element In : companies Out: 'Apple' In :
Nested tuples can be accessed like this.
In : num_tup = (1, 2, 7, [10, 12]) In : # get 12 from the tuple In : num_tup Out: 12 In :
2 . Negative Indexing –
You can also use Negative Indexing to access tuples in python. Negative indexing starts from -1 from the end of a tuple. To get the last item using negative indexing you have to write tuple[-1] to get the second last item tuple[-2].
In : companies Out: ('Google', 'Apple', 'Microsoft', 'Facebook') In : companies[-1] Out: 'Facebook' In : companies[-2] Out: 'Microsoft' In :
3 . Slicing –
You can also access multiple items from a tuple using slicing in python.
Syntax for slicing –
start – index from which you want to start
stop – index before which you want to stop
step – the step size. By default it is 1.
When you do slicing, start is included and end is excluded. Let’s say you want to get the middle two items Apple and Microsoft.
In : companies Out: ('Google', 'Apple', 'Microsoft', 'Facebook') In : # get Apple and Microsoft In : companies[1:3] Out: ('Apple', 'Microsoft') In :
If you want you can also omit any start or end index or both. The following code tells python to get everything from the start and stop before the 2nd index.
In : companies[:2] Out: ('Google', 'Apple') In :
And the following code say start from the 2nd index and go till the end.
In : companies[2:] Out: ('Microsoft', 'Facebook') In :
Omitting both the index means gives me everything from start to end.
In : companies[:] Out: ('Google', 'Apple', 'Microsoft', 'Facebook') In :
To get every other element we can use the step size.
In : companies Out: ('Google', 'Apple', 'Microsoft', 'Facebook') In : companies[::2] Out: ('Google', 'Microsoft') In :
As we discussed before that you can not change a tuple once it is created.
In : num_tup Out: (1, 2, 7, [10, 12]) In : num_tup = 10 Traceback (most recent call last): File "C:\Users\BHOLA\AppData\Local\Temp\ipykernel_27112\2085553011.py", line 1, in <module> num_tup = 100 TypeError: 'tuple' object does not support item assignment In :
But if the element itself is mutable like a list then you can change the element of the list.
In : num_tup Out: (1, 2, 7, [10, 12]) In : num_tup = 100 In : num_tup Out: (1, 2, 7, [100, 12]) In :