1 00:00:00,150 --> 00:00:08,450 In this tutorial we are going to discuss about another data structure that is Japanese. 2 00:00:08,490 --> 00:00:17,270 Biden provides another third type that is an ordered collection of objects called a tuple. 3 00:00:17,610 --> 00:00:24,930 Similar to lists lists are also unordered collection of objects. 4 00:00:25,020 --> 00:00:35,290 But the difference between tuple and lists is tuples immutable object types whereas lists are mutable. 5 00:00:36,170 --> 00:00:45,650 And since tuples immutable I iterating through a tuple is faster than the list so there is a slight 6 00:00:45,650 --> 00:00:47,180 performance boost. 7 00:00:49,710 --> 00:01:00,360 Next tuples are defined by enclosing the element in bed and this is separated by a comma so when we 8 00:01:00,360 --> 00:01:10,500 define a list we include these elements in square brackets and the elements are separated using a comma 9 00:01:11,280 --> 00:01:13,170 coming to a tuple. 10 00:01:13,170 --> 00:01:24,390 The element enclosed in parenthesis and they're separated by a comma next a tuple can have any number 11 00:01:24,390 --> 00:01:36,130 of items and they may be of different types so it can include strings numbers Lists Tuples etc.. 12 00:01:36,210 --> 00:01:44,040 If what you pull has only one element then the element should have a trailing comma to indicate that 13 00:01:44,160 --> 00:01:46,120 it's a tuple let us check. 14 00:01:46,170 --> 00:01:58,800 If we skip a comma after an element in a tuple what happens so here I have defined a tuple containing 15 00:01:59,250 --> 00:02:04,000 five elements in that enclosed in parentheses. 16 00:02:04,010 --> 00:02:11,860 Let us now check the data type of the variable to 1 2 1. 17 00:02:12,010 --> 00:02:25,750 So this is a tuple and if we want to define an empty tuple so the entity bill contains no element and 18 00:02:25,750 --> 00:02:28,910 it only contains the parenthesis. 19 00:02:28,960 --> 00:02:38,770 Now let's check the type of this variable to 1 so it is a tuple as well. 20 00:02:39,370 --> 00:02:50,350 Now we have just learned that if a tuple Contains a single element the element has to have a trailing 21 00:02:50,350 --> 00:02:51,450 comma. 22 00:02:51,550 --> 00:02:53,240 Let's execute this. 23 00:02:53,380 --> 00:02:57,120 Now check the type of this variable. 24 00:02:57,760 --> 00:02:59,820 So it this tuple as well. 25 00:02:59,860 --> 00:03:05,240 What if we remove the comma after the element. 26 00:03:05,260 --> 00:03:15,920 Now if we check the type of debatable says it is an integer so in order to tell Python that we want 27 00:03:15,920 --> 00:03:20,170 to define a tuple with a single element. 28 00:03:20,270 --> 00:03:31,480 We need to include the trailing comma since the element in a tuple in an ordered sequence just like 29 00:03:31,480 --> 00:03:42,150 list we can use the index operator to access an item in a tuple and the index as usual starts from zero. 30 00:03:42,340 --> 00:03:50,580 By then allows negative indexing for its sequences such as strings Lists Tuples. 31 00:03:50,590 --> 00:03:56,980 Also we can access a range of items in a tuple by slicing. 32 00:03:57,040 --> 00:04:03,240 So if we want to access any element in did tuple we can use indexing. 33 00:04:03,820 --> 00:04:12,340 So I want to index the element 4 and it has the index 3. 34 00:04:12,440 --> 00:04:22,280 So this is going to the tree the element for next I want to the tree the list which is a sub list defined 35 00:04:22,370 --> 00:04:24,620 in the tuple so 36 00:04:27,510 --> 00:04:33,810 in the square brackets and one the index number for and this is going to the tree. 37 00:04:33,820 --> 00:04:46,460 The list now if I want to access any element defined in this sub list in that case we use nested indexing 38 00:04:46,520 --> 00:04:52,810 or we need to place an additional index like this. 39 00:04:52,850 --> 00:04:58,730 So in this case being under the tree the string be. 40 00:04:58,790 --> 00:05:07,850 So here in the Jupiter notebook book I have defined the variable which is a tuple so in order to access 41 00:05:07,910 --> 00:05:13,970 items in the tuple we can use indexing. 42 00:05:14,480 --> 00:05:23,860 So I'm gonna parse the index number of 0 which has to then retrieve the first element in the tuple okay. 43 00:05:23,900 --> 00:05:35,060 Now if I want to access the sub list that is defined indictable in that case the sub list has index 44 00:05:35,060 --> 00:05:45,280 number for and if I want to access the element defined in the sub list we need to have an additional 45 00:05:45,280 --> 00:05:51,180 index nada to retrieve element in the sub list. 46 00:05:51,370 --> 00:06:01,990 So as you can see we can also access the element in the sub list are nested list next. 47 00:06:02,040 --> 00:06:06,750 Python allows for negative indexing tuples. 48 00:06:06,750 --> 00:06:18,810 So if I want to access the sub list again using negative indexing it has minus 1 as the index and this 49 00:06:18,810 --> 00:06:28,110 is going to retrieve the list in digital and we can also retrieve part or portion of it you build using 50 00:06:28,200 --> 00:06:29,010 slicing. 51 00:06:29,670 --> 00:06:38,700 So let's say we want to slice from the beginning up to the element in the fourth position. 52 00:06:38,730 --> 00:06:46,300 In that case the first index number should be applying and the second index number should be four. 53 00:06:46,310 --> 00:06:55,070 So this is going to retrieve the first fourth element in the tuple and slicing is going to create another 54 00:06:55,070 --> 00:07:04,620 tuple but there's not going to change the original Japan as you can see here at D or additional tuple 55 00:07:04,980 --> 00:07:07,860 has not changed even after slicing.