[no audio]
Friends, in this video
we are going to discuss about the next data structure,
that is tuple data structure, right.
See let me show you how to work with tuple,
I mean how to define, and how to access your values of your
tuple, right, how to do some operations on your tuple data
structure. First thing
let me define.
So how to define your
[no audio]
tuple, right?
See guys you can define your tuple, you know,
like 'my_tuple', 'my_tuple', any variable name you can
take but I am taking simply 'my_tuple', (3, 4, 5),
and if you want to define any empty tuple, right,
[no audio]
'my_empty_tuple=', this set.
Right. Now, let me save this as some python script, and I want
to run something here,
so 'working_with
[no audio]
_tuple'.
Fine.
So first thing, how you can print your tuple? Simply by variable
name. See the output. Or how you can print your empty tuple?
Whatever the variable you have for that,
that's it. Now see the output.
[no audio]
Right. Now, let me convert your
tuple into Boolean.
So first I am going to convert my empty tuple into Boolean.
You know the empty list if you convert into tuple, what you
are going to get? The Boolean data as 'False'. Same way here
I am converting my empty tuple into Boolean.
Now you're getting 'False'.
Let me convert some non-
empty tuple into Boolean. Non-empty is nothing but already you have
your tuple, right?
See the output. 'True', right.
So if we convert any empty tuple data into a Boolean you
will get 'False', or if you convert any non-empty tuple data into Boolean
you will get always 'True'.
Right. Fine.
The next thing is,
[no audio]
so I want to define a tuple
[no audio]
such that 'my_tuple' consists of different values,
and in that one of the value is a list.
Yes, you can. I mean suppose sometimes if you, based on requirement,
you can also define your tuple in this way.
Let's say (3, 4,), instead of 5, the third element
I am taking as a list, no problem you can take it.
[no audio]
Right. Now, let me define,
let me print my new tuple data. See the output, what you are getting.
[no audio]
Now guys, you know how to access your elements from your
list based on index, the same concept is applicable even while
working with your tuple.
You can access particular value from your tuple.
Let's say I want to access number 4. So, you know, if you
go with the positive index, the number 4 index is 1, right
because 3 is 0, then 4 is 1, then this entire thing is one element guys.
This entire thing is one element for your main tuple.
So that's why for this entire list, index is 2. Now,
this is 3 and this is 4.
Right. Now let me print something, 'print', your tuple with index 0.
[no audio]
See the output, what you are getting. You are getting first
value. Let me print tuple value on index 1. Now see the
output what you are getting.
Let me print the index value 2 from your tuple.
That is the list of value. See the output what you are getting,
[5, 6, 7]. Now from this if you want to get, right, suppose
6, number 6 I need to get. So if you want to get 6, first
thing you have to get this element, that is the list.
Yes, we are getting that with the help of index 2. Now from this,
from this you need 6 value.
That means again you have to take index 1. Now see the output
what you are going to get, 6. So be clear guys.
If you want to access digit 6, first of all from your main
tuple, this is your main tuple, from that tuple first
you have to get this element. To get that element
we are taking index as 2, because positive index 0, 1, 2. Now,
this is like one list, right. From this list
if you want to get second element, you have to take index as 1.
That's what I did.
[no audio]
Fine. Now, let me assign from my tuple, index 0 value something
is there, I want to modify that with some other value
let's say in this way. Observe that whether it is possible or
not. You are getting an error.
'tuple' object does not support item assignment, that means
once if you define the values in your tuple, you cannot
change any values or item in your tuple.
That means tuples are immutable. Assignment modifying the
part of your tuple is not possible.
[no audio]
But modifying the entire data is possible, like your
strings also, right. Even in your strings you can modify entire
string, but you cannot modify the part of your string.
Same rule is applicable with your tuple as well.
Right. So it's very, very important guys. Tuple
and strings are immutable. That means once if you define you
cannot change the part of your tuple or string, that's it.
Now tuple is not able to do, I mean you cannot
perform any operation such that
whatever the operation you are using, right, that operation
is not going to modify your tuple.
That's why you are saying tuples are immutable.
That's why if you see the operations on your tuple, there
are not much operations on your tuple.
Let's say I am defining tuple called (4, 5), and see
the operations on your tuple, only 'count', and 'index' operations.
You don't have like append, insert, update,
I mean top, remove, extend, those all are there with your list,
right? Let's say I have a list of data,
[no audio]
so on that I am trying to find the list of operations. See
that you have index, extend, sorry, index
anyway it is there with you in your tuple also, right, index and count.
Other than that, you don't have any operations like remove,
sort, right, insert, extend, copy.
[no audio]
So that means, once if you define a tuple you cannot modify
it, right. Now, you know how to use your 'count' and 'index'.
What is the purpose of 'count'? If you have a tuple like this,
something like this,
so how many times particular letter is there, particular number
is there, particular data is there in a given tuple?
Let's say I want to find 5, how many times it is there?
Right. Let's say I define one more 5 now, and check it, 2 times.
Then what is the index of number 5?
0. So is there any number 5 after index 1, sorry after
index 0? Because very first index is number 5, right?
I don't want to look for this
other than that. From here
I want to look, is there any 5, then provide the starting
index from where you want to look?
Yes, it is there somewhere in fifth position.
That's it, and you can find length of your tuple, guys.
See that six letters are there, I mean six data are there in your tuple.
So length is applicable to your strings, list, and tuple, right?
Let me show you once again.
If it is a 'list' then length of your, sorry, if it is a string,
then length of your string.
Let's define some list,
then length of your list.
Let's define some tuple,
[no audio]
then what is the length of your tuple? That's it.
[no audio]
Okay. So guys you can access based on index your tuple
values as your list, right?
I mean already we've done this thing
no, let me do it once again.
[no audio]
Come here. Let me define 'my_tuple' in this way.
Any tuple, no problem. Right.
[no audio]
Now, you know what is the use of 'tuple:',
same operation as your list, and string. You're going to get
entire tuple, but I want to get from index 0 to last, same
thing. But I want to get from index, third index to last. Third
index means, 0 index is 3 number,
this is second index, and this is the, sorry 0,
1, 2, 3, 6 is the third index, from 6 to last I want, then
I can take in this way.
Right. So guys, whatever the index operations we performed on your
strings all or applicable here as well.
Let's say I want to get from starting to 5th index value. If
you want to get 5th index value we have to provide 6, right?
See that.
1, first, 0, 1, 2, 3, 4, 5, 6,
[no audio]
if you take 6th index, you're
getting up to five values,
sorry six values up to 5th index, right?
That's it. Now,
let me take one more thing,
last operation,
[no audio]
one second.
[no audio]
So that is simply 'print(my_tuple)' from third
index, 3 values. From third index 3 values. Then 3 plus 3, 6
you have to provide or from third index
I want to get two values only, 3 and 4; 3 and 4.
That's why 3 plus 2, 5. Now
if you observe the output you are getting two values as a tuple.
[no audio]
Now we have last explanation with your tuple, that is,
[no audio]
see suppose if I write 'x=5', then 'print(x)', and then 'type(x)',
see the output. Integer, right?
But I am giving ',' here. Now see the output.
'Tuple'. So if we write 'x=5,' that is a
tuple which consists of only one value.
[no audio]
And also you can use something like this.
This is also valid tuple. 5, 8, 9, means a
tuple which consists of three values. See the output. You're
not getting any error.
If you write in this way, by default it will become a
tuple data structure.
You need to remember this point.
Okay, single data tuple is you can write something in this,
some number, but if you don't write ',' it will become a integer
data, integer variable, but it is not a tuple data structure
variable. If you write ',' then it will become a tuple data
structure variable. Right.
So guys, these are the important points with your tuple.
Guys, as of now we have seen a list data structure and
tuple data structure.
So you need to remember that lists are mutable. Whenever if
you want to change the data of your list, any part of your
list, you can change it, but you cannot change the
part of your tuple.
That's why lists are mutable,
and tuples are immutable,
okay. Then what is the usage of your tuples and
lists in your real life?
[no audio]
See you are designing some python script,
right, and you are taking some data.
So while taking data
by logic if you want to change your data, you have to take
your data as a list.
Even by mistake if you don't want to change your data then
simply consider your data as a tuple.
And we'll see that situation with the help of an example
once if we complete with your basic concepts.
So up till that you have to wait.
So up to that you have to remember simply tuple immutable,
lists are mutable. That's it.
Okay. Okay guys, thank you for watching this video.
[no audio]