00:00In today's video we'll have a look on the methods of
'Tuple' , which we can use to manipulate them
00:07And we'll have a look on a lot more stuff
00:09Let's move to the computer screen
And let's get started
00:21When we talked about 'List'
so we also talked about methods of 'Lists'
00:24We learned how we can manipulate 'Lists'
00:27Can we manipulate 'Tuple'?
00:30If you want to change, add or remove
a value from a 'Tuple'
00:34So there's no direct method to do that
00:36You have to copy it and make a list then
you have to do changes in that list
00:41And then you have to change
it again in 'Tuple'
00:42Because that's the main point of 'Tuple'
You cannot change it
00:45If a 'Tuple' contains 5 items so they'll remain 5
you cannot add 6áµ—Ê° item in it
00:50But we'll now have a look on methods which will tell you about the
elements of a 'Tuple' or give you more information about it
00:58As we learned
" 'Tuples' are immutable "
01:00" If you want to add, remove or change tuple items,
then first you must convert the tuple to a list. "
01:06And then you can perform functions like
'adding' 'removing' or 'changing'
01:11And again you can convert that
'List' into a 'Tuple'
01:14I have taken a quite good example here
And I want you all to have a look on
01:19Here's a 'Tuple' and
I've created a temporary 'List'
01:22And then I appended 'Russia' in it
01:25And then I've used a " .pop " method of 'List'
01:28Then I made some changes in that 'List' and
finally I converted that 'List' into a 'Tuple'
01:33So this is how you can change a 'Tuple'
indirectly, but not directly
01:38Now look, " Thus, we convert the tuple to a list,
manipulate items, then convert it back to a tuple. "
01:43" However, we can directly concatenate two
tuples without converting them to list. "
01:48Because we're creating a new 'Tuple'
01:49We are not changing an existing 'Tuple'
01:51But we're creating a new 'Tuple'
01:52When we are concatenating two 'Tuple'
we're creating a new 'Tuple'
01:58Now if you have a look on this example
02:01Because these example are already here
so I'm over-writing them
02:04You can copy & run them on your own
02:06So we've merged them both
02:09A new 'Tuple' has been created by merging the
names of these countries and these countries
02:15This is how we can do this
02:17Now there are some methods of
'Tuple' which you must know
02:20For eg. there's a " .count " method which shows
the number of occurrences of a given item
02:27Now look I've created a 'Tuple'
with name " tuple1 "
02:29Let me make it small because we
use 'Capital' for classes
02:33This not important for now but you'll know this
in 'Object oriented programming'
02:37I'll change this to small (t)
02:39There will be no difference but again it is a convention that
we use "Classes are written with capital letter"
02:44And variables are written with 'Small' letters
02:483 occurs thrice
that's why I got 3 as output
02:55The output is 3 and if I add another 3
so the output will be 4
02:58It'll show the number of occurrences
of a given item
03:06I hope you understood this
03:07Now " .index " method it is the same as 'List.index'
"Returns the first occurrence of the given element from 'Tuple' "
03:14It raise value error if the element is
not present in the list
03:21If I do something like this...
03:23If I write " res = tuple1.index(3) "
03:28We'll get the index of first occurrence of (3)
which is 3
03:33What if I change this to (31)?
So I'll get " 5 "
03:37Because it's on the fifth index
03:41This is what you must know
03:43Now there's another thing in this....
03:44I got the occurrence of (3)
03:47As I know there are a lot of
occurrence of (3) here
03:50But I want to know the index of (3)
from just this portion
03:53I can do it something like this
03:56That I can add starting point
03:58[0] [1] [2] [3] Let's suppose if I give 3
as starting point
04:01And then if I add ending point
[0] [1] [2] [3] [4] [5] [6] [7]
04:07Let me do one thing,
I'll change this to [4, 7]
04:11And now I want the index of (3)
04:15Firstly it'll do the 'Slicing' of this
'Tuple' from [4:8]
04:18Now it is saying " x is not in 'Tuple' "
04:20In-fact I made a mistake, this (3)
must be added in the starting
04:24(3) must be added in the starting
04:25It'll first do slicing of the
'Tuple' as [4:8]
04:28When it'll do slicing of this 'Tuple' with [4:8]
[0] [1] [2] [3] [4] [5] [6] [7]
04:36[4:8] will contain this
04:38So look, what will be the occurrence of (3)?
04:42What will be the index of (3)?
04:46It'll be 5
[0] [1] [2] [3] [4] [5]
04:49In original 'Tuple' 3 is on the fifth index
04:53What if I change this to (31)?
04:56Then I won't get " 5 " but " 7 "
04:59Is the output is 7?
Yes it is
05:01Because it is on 7áµ—Ê° index
05:04[0] [1] [2] [3] [4] [5] [6] [7]
here's 3
05:07That's why we'll get 7
05:09You'll use " .index " method quite a
multiple times in your life
05:14But you'll rarely use the starting variation
05:17You'll use this one a lot of times
05:18But again I just told you how it works
05:22Remember it'll raise value error if the
element is not present in the 'Tuple'
05:25For eg. If I do something like this....
05:27If I change this to (311) from (3)
05:31So it'll raise a value error
05:33It'll display "There's nothing like (311) in your
'Tuple' , why are you trying to print it? "
05:37It raise value error as we saw just now
05:40Then comes " .length " in tuple's methods
05:42If I want to show you the length of this 'Tuple'
05:48I'll add 'tuple1' here
05:50Let me comment this out
05:51And I'll get it's length
05:54These were some methods of 'Tuple'
which I want to explain to you
05:58You can use all methods of 'Lists' once you
converted a 'Tuple' to a 'List'
06:02And then make the changes wanted to make
06:06Then convert it again to the 'Tuple
06:09As long as it'll remain in the state of 'Tuple'
You won't be able to change it
06:13But when you converted it into a 'List' from
a 'Tuple' then you can make changes
06:16Then after making changes if you
convert it again to a 'Tuple'
06:19Then you cannot make change
in existing 'Tuple'
06:22Because 'Tuples' are immutable
06:24I hope you understood this
06:27And if you haven't accessed the play-list yet
so make sure to do it right now
06:31Bookmark it by clicking here
06:33Save it by clicking here
06:34That's it for this video guys
06:36Thank you so much guys for watching this video :)
06:38And I'll see you next time