In this tutorial we are going to discuss about another data structure that is Japanese.
Biden provides another third type that is an ordered collection of objects called a tuple.
Similar to lists lists are also unordered collection of objects.
But the difference between tuple and lists is tuples immutable object types whereas lists are mutable.
And since tuples immutable I iterating through a tuple is faster than the list so there is a slight
performance boost.
Next tuples are defined by enclosing the element in bed and this is separated by a comma so when we
define a list we include these elements in square brackets and the elements are separated using a comma
coming to a tuple.
The element enclosed in parenthesis and they're separated by a comma next a tuple can have any number
of items and they may be of different types so it can include strings numbers Lists Tuples etc..
If what you pull has only one element then the element should have a trailing comma to indicate that
it's a tuple let us check.
If we skip a comma after an element in a tuple what happens so here I have defined a tuple containing
five elements in that enclosed in parentheses.
Let us now check the data type of the variable to 1 2 1.
So this is a tuple and if we want to define an empty tuple so the entity bill contains no element and
it only contains the parenthesis.
Now let's check the type of this variable to 1 so it is a tuple as well.
Now we have just learned that if a tuple Contains a single element the element has to have a trailing
comma.
Let's execute this.
Now check the type of this variable.
So it this tuple as well.
What if we remove the comma after the element.
Now if we check the type of debatable says it is an integer so in order to tell Python that we want
to define a tuple with a single element.
We need to include the trailing comma since the element in a tuple in an ordered sequence just like
list we can use the index operator to access an item in a tuple and the index as usual starts from zero.
By then allows negative indexing for its sequences such as strings Lists Tuples.
Also we can access a range of items in a tuple by slicing.
So if we want to access any element in did tuple we can use indexing.
So I want to index the element 4 and it has the index 3.
So this is going to the tree the element for next I want to the tree the list which is a sub list defined
in the tuple so
in the square brackets and one the index number for and this is going to the tree.
The list now if I want to access any element defined in this sub list in that case we use nested indexing
or we need to place an additional index like this.
So in this case being under the tree the string be.
So here in the Jupiter notebook book I have defined the variable which is a tuple so in order to access
items in the tuple we can use indexing.
So I'm gonna parse the index number of 0 which has to then retrieve the first element in the tuple okay.
Now if I want to access the sub list that is defined indictable in that case the sub list has index
number for and if I want to access the element defined in the sub list we need to have an additional
index nada to retrieve element in the sub list.
So as you can see we can also access the element in the sub list are nested list next.
Python allows for negative indexing tuples.
So if I want to access the sub list again using negative indexing it has minus 1 as the index and this
is going to retrieve the list in digital and we can also retrieve part or portion of it you build using
slicing.
So let's say we want to slice from the beginning up to the element in the fourth position.
In that case the first index number should be applying and the second index number should be four.
So this is going to retrieve the first fourth element in the tuple and slicing is going to create another
tuple but there's not going to change the original Japan as you can see here at D or additional tuple
has not changed even after slicing.