Followers

Wednesday, January 8, 2020

Multiple Assignment - Variables and Assignment

Hello friends, Welcome to our website. Today we will talk about Multiple Assignments in Python. 

To understand this New topic you need to First read our last post which was on :

Multiple Assignment

Python is very versatile with assignments. Let's see how different ways, you can use assignments in Python. 
  • Assigning some value to multiple variables : 
        You can assign the same value to multiple variables in a                       single                        statement, e.g.,    
                  
                    a = b = c = 10
         It will assign value 10 to all three variables a,b,c. That is all three labels a,b,c will refer to the same location with value 10. 
  • Assigning multiple values to multiple variables : 
         You can even assign multiple values to multiple variables in a single statement, e.g.,  
              x, y, z = 10, 20, 30 

It will assign the values orderwise, i.e., the first variable is given first value, second variables the second value so on. That means, the above statement will assign value 10 to x, 20 to y, 30 to z.
This style of assigning values is very useful and compact. For example, consider the code given below : 
              x, y = 25, 50 
              print(x, y)

It will print result as, 
    25    50 
Because x is having values 25 and y is having values 50. Now, if you want to swap values of x and y, you just need to write : 
              x, y = y, z 
              print(x, y)

Now the result will be 
     50     25 
Another way to swap values is to take the third variable and I have done that method in one of our posts just CLICK HEREWhile assigning values through multiple assignments, please remember that Python first evaluates the RHS [Right Hand Side] expression(s) and then assigns them to LHS, e.g., 
                  a,b,c = 5,10,7                   # Statement 1    
                  b,c,a = a+1, b+2, c-1       # Statement 2
                  print(a, b, c)

  • Statement 1 assigns 5 ,10, 7 to a, ,b, c respectively.
  • Statement 2 will first evaluate RHS i.e., a+1, b+2, c-1 which will yield 
             5+1, 10+2, 7-1, = 6, 12, 6 
           Then it will make the statement (by replacing the evaluated               result of RHS)as :
               b, c, a = 6, 12, 6

  • The third Statement will print 
              6  6  12
Now guess the output of the following code fragment 
       p, q = 3, 5
       q, r = p-2, p+2 
       print(p, q, r)
Pease note the expressions separated with commas are evaluated from left to right and assigned in same order e.g.,
           x = 10
        y, y = x+2, x+5
will evaluate to following (after evaluating expressions on RHS of =  operator)
          y, y = 12, 15
i.e., firstly it will assign first RHS value to first LHS variable i.e.,
            y = 12
then it will assign second  RHS value to second LHS variable i.e.,
            y = 15 
So if you print y after this statement y will contain 15.
Now consider the following code and guess the output :
                    x, x = 20, 30
                    y, y = x+10, x+20
                    print(x, y)
Well it will print the output as 
          30   50   
Now you may know why?
  
So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that i upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!
Vande Mataram!!!

No comments:

Post a Comment

If you have any doubts, please do let me Know.