In this tutorial we will discuss about the topics packing and unpacking DuPont's packing is a simple
syntax which lets us create two booths without using the conventional notation that is defining a tuple
without the parenthesis and coming to unpacking Python tuples unpacking happens when we assign while
use from a tuple to a sequence of variables.
So let us understand packing and unpacking using some examples
so here I have defined a tuple without using the conventional notation that means I have defined a sequence
of elements separated by commas without dependencies when we define tuple like this python.
Create a tuple of four values and then assigns it to eat.
So this is what happens python in the background.
Create a tuple like this by backing.
So this is known as tuple packing.
So here I have defined 4D variables name age height and weight in these variables in a tuple have been
assigned to eight this assignment statement is equal in two 4D assignment statements does when we do
this assignment python is going to unpack the values in did you pull a and then assign the values to
the variables defined to the left of the equal to sign.
So these are the values in did you pull a John
seven is the age height is three feet then weight is 20 kilos.
So this assignment statement here at unpacks these values in a and then assigns these values to do that
ability in remembered the number of values in a tuple have to be equal to the number of variables that
we have defined to the left of the equal to sign.
The concept of tuple packing and unpacking could be used for swapping while used say for example I have
defined two variables X and Y.
X contains the value to whereas y contains three.
So the values in X and Y can be swapped using this statement.
And python in the background.
Forced convert these sequence of values to a tuple so it first backs the values into a tuple and Y indexed
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
the value in y that history is assigned to debatable X the value 2 is assigned to the variable right
now x contains three and Y contains two.
So we have swapped values using tuple packing and unpacking.
So here I have defined a variable a containing four elements separated by commas and once I execute
this line Python would pack all of these values into a tuple.
So let us now display a so a is a triple as you can see it has been enclosed in parentheses.
Now I'm going to define four variables name and then age height and weight in a tuple and assign this
to the two.
Eight as we know this single statement here is equal to four different but four separate assignment
statements.
That means the values in e are assigned to the variables that we have defined to hold and remember the
number of variables on the left should be equal to the values.
That is the values in tuple 8.
So let's execute this line and then check the value assigned to the variable name the variable name
has been assigned the value John or the string.
John H is 7 8 is 3 feet right.
So the values have been unpacked from the tuple a and then assigned to the variable on the lift.
And this is known as tuple unpacking now say for example you define your defined three variables in
a tuple here and then assigned to the tuple eight.
So when you execute this line of code this is the letter that you get.
So while you idiot and it says too many values to unpack and then we have also discussed about swapping
of values.
So let's define known to variables x and then Y.
Now we're going to swab devalues the values in debatable X and Y.
So this statement on as discussed it is going to back the values in white comma X as you can see here
and then this tuple is going to be unpacked in the next step and then assigned to the variables x and
y that are to the left side of the equal to sign no let's display X X is three and Y is two.