Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to discuss about our next data structure, which is called as tuples.
Already in our previous video we have seen about list and we had written a lot of code.
You know, if you probably I go ahead and show you we had written somewhere around 54 lines of code.
We understood about list comprehension.
We saw different different methods, operations and many more things.
Now, uh, in this session, what all things we are going to discuss.
So I have actually given you the brief outline of the video.
First of all, we are going to understand about tuples, how to create tuples, accessing tuple elements,
tuple operation.
What is this immutable nature of tuples.
And what are some of the, uh tuples common tuple methods that are used.
How to pack and unpack tuples, nested tuples and all these things.
We are going to see with a lot of practical example.
Now before I go ahead, first of all, let me just give you a small definition about tuples.
Right.
So tuples are ordered collection of items that are immutable.
Right.
So we'll understand about this particular meaning which is called as immutable.
If I talk about list it is an ordered collection of items.
And the items can be of any type.
And they are mutable right.
But here it is something like immutable.
They are similar to list, but they are immutable.
Their immutability makes them different.
Now first of all, we'll try to understand how to go ahead and create a tuple.
So for creating a tuple right we have to just use this two empty brackets.
So let me just go ahead and write it over here.
So this is my empty tuple okay.
And just by using this two brackets right I will be able to create it.
Right.
And if I go ahead and display the print, uh, let me first of all print this empty tuple.
And then we will also go ahead and display our type of this particular empty tuple.
Okay.
So here you have this.
If I execute this you can see this is an empty tuple that is getting created in list.
We use two square brackets.
In this we are just using this rounded brackets Right.
And then over here you can see that I'm getting an empty tuple which is completely empty.
And if I go ahead and see the type of empty tuple, it is nothing but tuple itself, right?
Now if I probably consider list right.
So list if I also use like this.
Right.
And if I go ahead and create an empty list.
So I can also use this class directly and create an empty list.
Okay, so this I missed in my previous video.
So I'm just going to show it over here.
And similarly if I want to probably create a tuple let's say this is my tuple, I can actually go ahead
and use the same type of class and create an empty tuple.
So both the ways I'm actually trying to show you so that you understand both these ways.
Right.
So here you can see when I'm seeing the type of this, this will be an list and this will be tuple.
Right.
So you can also use this um uh method in order to create an empty list or tuples.
Okay.
Inside this also I can put up elements.
It is up to us right now.
Let me go ahead and add some elements into this particular tuple okay.
Now how do I add this particular elements.
Uh uh inside this particular tuple.
And uh how to do that.
We'll be seeing a lot of methods.
But here what I'm actually going to do, let me just go ahead and directly create a tuple.
And if I go ahead and write tuple okay.
And here I can actually give a list like 123456 okay.
And now this list will get basically get converted into a tuple.
So if I go ahead and display this numbers here, you can see that I'm actually getting a tuple right.
So that basically means a list can be converted into a tuple.
And even a tuple can be converted in a list.
Right.
So if I go ahead and basically write like this 123456.
Right.
So this is my uh tuple itself.
And now if I go ahead and write list and I close this particular bracket.
So here you'll be able to see that I am able to get a list right.
List of elements.
So both of the ways uh, you can basically do the type conversion okay.
Now as I said, uh, in tuples also you can let's say I'm going to probably go ahead and create my mixed
tuple here.
Also you can use different different data type elements.
Let's say I want to use numerical I want to probably use string.
I want to go ahead and use a floating point number.
I can also go ahead and use my boolean variable.
Right.
So now if I go ahead and print this then also this will get uh created.
The tuple will basically get created.
Okay.
Now this is uh a basic information about tuple, how you can actually create and how you can proceed.
Okay.
Now let me do one thing quickly.
Let me go ahead and explain you about the next thing that we are going to discuss.
It is nothing but accessing tuple elements.
Okay.
Now for accessing tuple elements.
What all things we really need to do that we will be discussing.
Okay.
Like I want to pick up any elements as such.
So right now let me just go ahead and see.
This is my numbers.
Now here you can see that this is in the form of tuples.
I have six elements.
Let's say I go ahead and write numbers of zero okay.
Or let me just go ahead and write it in the land line next line.
So if I go ahead and write numbers of zero.
So here I'll be able to see first element over here.
This is my first zeroth index.
I'm able to see it over here.
Then I have two over here also.
You can see I'm able to pick up this elements right.
Similarly if I go ahead and let's say that I'm just going to print this okay I want to pick up the last
element okay.
Last element.
Either you can directly write the index or what I'm actually going to do, I'm just going to use minus
one.
Right.
So I'll be able to get the last elements again.
Uh the indexing will be almost similar like a list.
Even the slicing operation will be also similar, like a list.
Okay.
So let's say, uh, I go ahead and write numbers and here I'll write zero colon four.
That basically means I'll be able to get the element between zero index to the third index.
So same slicing operation whatever things we have actually discussed.
Similarly, if I go ahead and write numbers with double colon right I'll be able to see it.
If I go ahead and write double colon minus one, I'll be able to get the reverse of all the tuple values
right, but the return type will be always tuple.
Okay, so all the indexing and slicing operation that we have actually done with the help of list.
Similarly, you can actually do it with the help of tuples.
Okay then let's go ahead and see tuples operation tuple operations.
Okay.
Now tuple supports various operations like concatenation repetition and slicing okay.
So we'll be discussing about that.
Let's say I have numbers one tuple okay.
And I had also created another tuple which is called as mixed tuple.
Right.
So this were the elements right.
Now what if I just go ahead and write something like this.
Numbers plus mixed tuple okay.
So here in short this is an arithmetic operation.
But since we have two tuples over here I will definitely say this as a concatenation, right?
Concatenation basically means combining two things, right?
So here you'll be able to see I will just go ahead and write concatenation underscore tuple.
And I will just go ahead and concatenate this.
Now if I go ahead and print this concatenation right concatenation tuple, you'll be able to see that
I will be getting something like this.
Now all this tuple both the elements of this particular tuples has been combined.
So I have 1234, five six then one hello world 3.14 and true.
Okay, now uh, let's say that I want to perform some more operations, right?
Let's say I want to go ahead and write something like this, okay.
Or repeated or let me do one thing.
Let me just go ahead and create this.
So I will just go ahead and do okay.
Or I'll take this mixed tuple.
Okay.
And I will just try to multiply with an operation three.
Now see what will happen.
So here you can see mixed tuple had one.
Hello world 3.14 and true.
Now this has got entirely repeated three times right.
So in this kind of multiplication operation what is basically happening is that the entire tuple is
basically getting appended three times.
Right.
So that that is the reason we are basically doing an operation over here.
So, uh, so this is what is something different with respect to the calculation that we do.
Now you may be thinking, Krish, what about if I try to do this with numbers right.
Numbers multiplied by three.
So here also the same thing will happen.
So 123456123456.
And 123456.
You may be thinking, Krish, wasn't it like you have to probably multiply all these numbers that also
we can do it, but we have to iterate it inside this particular tuple, get each element and then perform
numerical operation.
Okay.
But right now with respect to generic concatenation and this kind of operation here you can see that
three times n number of times based on this multiplication that we are doing that many number of times
this tuple will get element are appended with respect to the element.
Okay.
So these are some of the basic operations.
Now let's go ahead and discuss about immutable nature of tuples.
Okay.
This is important of tuples.
Now in the immutable immutable nature of tuples you need to understand one thing.
And that is the first point that we have already learned right.
Tuples are immutable.
Okay.
Meaning the elements cannot be changed once assigned.
Okay, let's say that I go ahead and create my list and I go ahead and write one, two, three, four,
five.
Right.
Now let me do one thing.
List of zero.
Or if I go ahead and print this specific list, uh, I will be okay.
There is an error.
Don't worry.
Okay, so here you can see I'll be getting this specific element.
Now let's see this okay.
Now what I will do I will go ahead and take the first index and replace with it.
Crush okay.
Now I'll go ahead and print the list.
Now see what will happen.
So here we are changing the element.
And this second index or this first index element has got replaced with crush.
So this basically means uh list are mutable.
That basically means the element can be changed once assigned.
Now similar operation if I try to do with the same numbers that we had, write in the form of tuple.
If I go ahead and say hey, number of one, what is the number of one two?
If I try to assign some value, let's say I try to assign a value called as Krish.
Here it says that hey tuple object does not support item assignment.
And this is what immutable immutability comes into picture.
Tuples are immutable.
The elements cannot be changed once assigned.
Okay So here you can actually see what I exactly mean to say.
Right.
And this is very much important.
That basically means once you have defined a tuple you cannot change the element inside that okay.
Once it is assigned there are ways how you can do it.
First of all, you can take the tuple, convert into a list, and then change the element and then finally
convert back again to the tuple if you want.
Okay, that is how you can actually do okay, But by default, uh, you'll be seeing that tuples are
immutable.
Perfect.
So till here we have understood.
Now let's go ahead and discuss about tuple methods okay.
And now in this tuple methods uh we will be seeing uh two important methods in tuple.
So let's go ahead and uh see this.
So let's say this is my tuple number.
Uh so one of the, one of the example that I would like to show you, or one of the method that is commonly
used with people.
And there are only two methods that we specifically use.
Okay.
Numbers dot count.
Okay.
Let's say I want to count how many number of times one is present okay.
So let me just go ahead and see this.
And it is only present one time.
Let me just go ahead and make a code over here.
And let me just display the numbers.
I have 123456 okay.
So this count function is like it'll just try to count how many number of times the element.
The element that I have given over here is present in the specific tuple.
The next thing is that I will go ahead and print uh numbers.
Dot.
Let me just go ahead and do one function which is called as index.
Now let's say I want to understand what is the element present in the index.
Third.
Right.
And if I go ahead and execute here, you can see that in the third index 0123.
Right.
Or sorry what is the element like.
Uh what uh where is this third element present.
Like in which index this third element is present.
Right.
Or this three is present.
Uh, again let me repeat it.
Um, numbers dot index.
Index is a function over here.
And with respect to index, what it does is that it returns the first index of the value, okay.
Whatever value I'm actually giving you, it is just going to return the index of that value.
So here you can see 012.
So three is basically present in the second index okay.
So I hope you have got this two methods easily and you have understood it.
One is count and one is index.
And these are the two common methods that are specifically used with tuples.
Okay.
Now now let me talk about one amazing thing okay.
And that is basically called as packing and unpacking tuple okay.
Packing and unpacking tuple.
Again this will be really handy whenever you are developing projects, whenever you are writing the
code.
Right.
So let me just go ahead and write packed underscore tuple is equal to and I will define the elements
like this one comma.
And let's say this is my string.
So this will be like hello.
Then I have 3.14 okay.
And I will just go ahead and print my packed underscore tuple.
Now once I do this here you'll be able to see that I'm able to get it.
Now whenever I define the numbers in this way like comma separated right by default, what it is going
to do is that whatever number of elements that I have given, it is going to pack them in a tuple itself,
right?
So that is the reason we say it as we are packing a tuple whenever we give this.
Now how do we unpack a tuple.
So this is with respect to packing okay packing.
But how do we unpack it.
So unpacking a tuple and how do we do it.
So here we know that there are three elements inside this tuple.
Let's say I have four elements of five elements.
Let's say in this case there are three elements.
So I am just going to create three variable.
Let's say a b c is equal to.
And here what I'm actually going to do I'm going to use the same PAC tuple okay.
Now what will happen if I go ahead and print a.
Okay.
And let me just go ahead and print B.
And similarly let me just go ahead and print C.
So here you can see one hello.
And 3.14 right.
So you are able to see this.
And this is nothing but it is called as unpacking a tuple right now.
Uh there is also some very important things, uh, that you really need to understand.
Okay.
Uh, with respect to unpacking, we can also use star.
Okay.
So let's say unpacking with star okay.
And this is something new that you will be able to see as new new Python version will be coming.
Some of the amazing features.
So let's say I have a numbers and in these numbers I define a tuple one, two, three, four, five
okay and six.
Okay.
Now let me do one thing.
I will create one variable like this.
And I'll use star.
Let's say star will be middle and my third variable will be last.
Okay, now why I'm creating this, you'll be able to understand.
Now see if I go ahead and display the number, or I'm just going to assign this particular numbers over
here.
Now let me just go ahead and print first.
Then I will go ahead and print middle okay.
Sorry I'll remove the star over here I'll not require star.
And then finally I will print the last element Okay, now see what will happen.
Over here.
I know six elements are there, but I'm just using three variables just to unpack.
And I'm unpacking with one star I'm using in the middle variable.
One star.
Okay.
Now see what will be the output okay.
So the first one has given me one.
This is fine.
Now the middle star what it has done right.
The star middle.
It has taken all the remaining elements till the last element.
See C23, four, five.
And the last number is basically giving you the sixth.
Right.
So what the star does is that from wherever you are probably starting right till the last element,
right before that last element, all the elements will be picked up.
Okay.
So this was about unpacking.
Three variants have actually shown you packing unpacking a tuple and pack unpacking with a star.
Okay.
Uh, let me just go ahead and create more cells so that you'll be able to, uh, will be able to execute
more code.
Okay.
Now let's go ahead and see some example for next tuples.
Okay.
Tuples inside a tuple is basically called as nested tuples.
List inside a list is also called as nested list.
So let's say if I go ahead and write like this, uh I can go ahead and create any number of list inside
a list.
Okay.
Let's say I have elements like one, two, three, four.
Here I have, um, six comma, seven comma, eight comma nine.
And let's say I have one comma, one comma.
Hello, comma.
Uh, hello, comma 3.14 comma.
Uh, see.
Okay.
Now this is what this is.
List inside a list.
Right.
So here is an example of nested list okay.
Because see I do not take in the previous session of list.
The reason is only that whenever is the right opportunity right.
We should include this topic over there.
Right.
And that is how you'll be able to understand all the other topics itself.
Right?
Because whatever operations we specifically do in list, similarly, only, uh, we also do it uh,
in tuple also with respect to accessing the elements.
And the difference is immutability and mutability.
Right now let me just go ahead and write list of zero.
Now list of zero basically means what this is my zeroth index.
This is my first index.
This is my second index okay.
So if I'm writing list of zero you will be able to see that I am able to get one, two, three, four.
But let's say that I want to pick up third element.
Now to pick up third element, what I really need to do, I have to use another index.
And this time this will be nothing but zero one, two.
So this will basically belong to my second index.
Right.
So here you can probably see that I am able to get the third element right.
So this is some common example right.
Let's say I want to also use slicing operation I want to get the elements between one till four okay.
So I can also go ahead and write like this zero colon.
Sorry zero colon three okay.
If I go ahead and write it.
So see I am able to get 123.
So slicing operation can also be applied in nested list okay.
List within a list okay.
Uh and it is not like only you need to have a list.
You can also have tuples.
You can also have multiple things.
Right.
So let's say I am going to probably go ahead and define this as tuple okay.
Tuple okay.
Or let me just show you in another example so that this code remains and I'll paste it over here okay.
And let me do one thing.
And here you have this.
And here you have this okay.
Now if I'm executing this let's say I want to probably get from my second index 012 okay.
Second index.
And let's say I want all the elements between zero to third index.
So here I'll be able to get one hello world and 3.14.
So inside a list also you can have tuples okay.
It is up to you however you want to create.
But whenever we talk about nested tuples uh let me just give you one basic example.
Here.
We are just going to create tuples inside a tuple.
So I have 123ABC true false okay.
So these are my tuples over here.
Now we will try to access the elements.
Access the elements.
Access the elements inside a tuple.
Okay.
Now let me just go ahead and write print nested underscore tuple of zero.
So if I go ahead and execute it I'm able to get 123 okay.
If I want to just pick up C let's let's consider that I want to just go ahead and pick up C.
Then how do I write I'll go ahead and write print nested underscore tuple.
And here I'm going to specifically write uh zeroth uh first index.
Right.
First index will basically get me all the elements over here.
And this will be my second index.
Right.
So here if I go ahead and execute it I'm able to get C right.
I can also iterate through this nested tuples right by using a for loop.
So iterating iterating over nested tuples.
Okay.
And here you'll be able to see for sub underscore tuple in nested underscore tuple.
Okay I'll first of all go to the nested tuple.
And then I will take the item item in the sub tuple.
And let me just go ahead and print it.
Let's print the item one by one.
Right.
So print item and here I'm just going to write end is equal to with a blank statement.
So that whenever it is printing it should print um along with this blank a new line also.
So now if I go ahead and execute see first tuple is basically getting printed one, two, three a,
b, c and then true and false.
So that is what I have right.
So this was example with respect to uh tuples we have seen different different operations.
We have seen about packing and packing.
Uh we have seen the inbuilt methods.
We have seen why do we get an error.
What is mutable immutability.
Everything we have actually seen.
And we have also seen some of the examples with respect to uh, list.
Right.
But overall if you probably see like tuples are a kind of data structure, again, it will be specifically
used when you do some kind of data analysis and all, uh, some or the other way, whenever you are
writing the code and all, you will be using this tuple extensively.
Now let me do one thing.
Let me go ahead and finally write the conclusion so that you have this conclusion in mind where all
tuples are tuples are versatile and very useful in various in many real world scenarios where an immutable
and ordered collection of items is required.
They are commonly used in data structure, function arguments, and return values as dictionary keys.
Understanding how to leverage tuples effectively can improve the efficiency and readability of your
Python codes, so I hope you like this particular video.
This was it for my side.
I will see you all in the next video, where we will be learning about the next data structure that
is called as sets.
Thank you.
Take care.
Bye bye.