1 00:00:00,120 --> 00:00:10,300 In this tutorial we will discuss about the topics packing and unpacking DuPont's packing is a simple 2 00:00:10,310 --> 00:00:21,070 syntax which lets us create two booths without using the conventional notation that is defining a tuple 3 00:00:21,610 --> 00:00:31,840 without the parenthesis and coming to unpacking Python tuples unpacking happens when we assign while 4 00:00:31,840 --> 00:00:36,120 use from a tuple to a sequence of variables. 5 00:00:36,220 --> 00:00:42,640 So let us understand packing and unpacking using some examples 6 00:00:46,370 --> 00:00:56,570 so here I have defined a tuple without using the conventional notation that means I have defined a sequence 7 00:00:56,570 --> 00:01:06,490 of elements separated by commas without dependencies when we define tuple like this python. 8 00:01:06,500 --> 00:01:13,010 Create a tuple of four values and then assigns it to eat. 9 00:01:13,880 --> 00:01:17,600 So this is what happens python in the background. 10 00:01:17,600 --> 00:01:23,210 Create a tuple like this by backing. 11 00:01:23,340 --> 00:01:26,520 So this is known as tuple packing. 12 00:01:26,520 --> 00:01:36,540 So here I have defined 4D variables name age height and weight in these variables in a tuple have been 13 00:01:36,600 --> 00:01:48,690 assigned to eight this assignment statement is equal in two 4D assignment statements does when we do 14 00:01:48,690 --> 00:01:59,550 this assignment python is going to unpack the values in did you pull a and then assign the values to 15 00:01:59,580 --> 00:02:03,510 the variables defined to the left of the equal to sign. 16 00:02:04,230 --> 00:02:10,260 So these are the values in did you pull a John 17 00:02:13,150 --> 00:02:24,030 seven is the age height is three feet then weight is 20 kilos. 18 00:02:24,430 --> 00:02:36,310 So this assignment statement here at unpacks these values in a and then assigns these values to do that 19 00:02:36,310 --> 00:02:45,850 ability in remembered the number of values in a tuple have to be equal to the number of variables that 20 00:02:45,850 --> 00:02:49,870 we have defined to the left of the equal to sign. 21 00:02:49,960 --> 00:02:58,600 The concept of tuple packing and unpacking could be used for swapping while used say for example I have 22 00:02:58,600 --> 00:03:01,400 defined two variables X and Y. 23 00:03:01,540 --> 00:03:07,020 X contains the value to whereas y contains three. 24 00:03:07,090 --> 00:03:13,710 So the values in X and Y can be swapped using this statement. 25 00:03:14,290 --> 00:03:17,330 And python in the background. 26 00:03:17,390 --> 00:03:30,040 Forced convert these sequence of values to a tuple so it first backs the values into a tuple and Y indexed 27 00:03:30,070 --> 00:03:44,980 contain the values 3 and two and here X and Y and the next step is to unpack the values in y and x so 28 00:03:47,410 --> 00:03:58,490 the value in y that history is assigned to debatable X the value 2 is assigned to the variable right 29 00:03:59,360 --> 00:04:07,480 now x contains three and Y contains two. 30 00:04:07,690 --> 00:04:14,070 So we have swapped values using tuple packing and unpacking. 31 00:04:14,140 --> 00:04:25,690 So here I have defined a variable a containing four elements separated by commas and once I execute 32 00:04:25,740 --> 00:04:32,790 this line Python would pack all of these values into a tuple. 33 00:04:32,880 --> 00:04:42,460 So let us now display a so a is a triple as you can see it has been enclosed in parentheses. 34 00:04:42,480 --> 00:04:57,690 Now I'm going to define four variables name and then age height and weight in a tuple and assign this 35 00:04:57,690 --> 00:04:58,540 to the two. 36 00:04:58,780 --> 00:05:10,410 Eight as we know this single statement here is equal to four different but four separate assignment 37 00:05:10,410 --> 00:05:11,590 statements. 38 00:05:11,670 --> 00:05:22,710 That means the values in e are assigned to the variables that we have defined to hold and remember the 39 00:05:22,710 --> 00:05:28,230 number of variables on the left should be equal to the values. 40 00:05:28,650 --> 00:05:31,070 That is the values in tuple 8. 41 00:05:32,010 --> 00:05:42,000 So let's execute this line and then check the value assigned to the variable name the variable name 42 00:05:42,030 --> 00:05:45,510 has been assigned the value John or the string. 43 00:05:45,540 --> 00:05:58,560 John H is 7 8 is 3 feet right. 44 00:05:58,700 --> 00:06:06,960 So the values have been unpacked from the tuple a and then assigned to the variable on the lift. 45 00:06:06,980 --> 00:06:15,990 And this is known as tuple unpacking now say for example you define your defined three variables in 46 00:06:15,990 --> 00:06:18,930 a tuple here and then assigned to the tuple eight. 47 00:06:19,230 --> 00:06:25,870 So when you execute this line of code this is the letter that you get. 48 00:06:25,870 --> 00:06:37,300 So while you idiot and it says too many values to unpack and then we have also discussed about swapping 49 00:06:37,420 --> 00:06:38,590 of values. 50 00:06:38,590 --> 00:06:45,960 So let's define known to variables x and then Y. 51 00:06:45,970 --> 00:06:53,480 Now we're going to swab devalues the values in debatable X and Y. 52 00:06:53,770 --> 00:07:07,820 So this statement on as discussed it is going to back the values in white comma X as you can see here 53 00:07:08,450 --> 00:07:17,180 and then this tuple is going to be unpacked in the next step and then assigned to the variables x and 54 00:07:17,180 --> 00:07:29,280 y that are to the left side of the equal to sign no let's display X X is three and Y is two.