Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8915115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:54:00+00:00 2026-06-15T04:54:00+00:00

I have declared four variables [a=1,b=2,c=3,d=0] in python and swapping them in one line

  • 0

I have declared four variables [a=1,b=2,c=3,d=0] in python and swapping them in one line code using ‘,’ and ‘=’ (Simple Assignment Operator).

I have got multiple Answers and got confused. please help me…

Case 1:

a=1
b=2
c=3
d=0
a=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)

Output of Case 1:

a = 2
b = 3
c = 3
d = 0

Case 2:

a=1
b=2
c=3
d=0
b=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)

Output of Case 2:

a = 2
b = 3
c = 3
d = 0

Case 3:

a=1
b=2
c=3
d=0
c=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)

Output of Case 3:

a = 2
b = 3
c = (2,3)
d = 0

Case 4:

a=1
b=2
c=3
d=0
d=a,b=b,c
print "a = " + str(a)
print "b = " + str(b)
print "c = " + str(c)
print "d = " + str(d)

Output of Case 4:

a = 2
b = 3
c = 3
d = (2,3)

Confusion is:

In the case number 3 and 4 the output is correct (as i expected). but in the case number 1 and 2 the value of a is 2 and value of b is 3. I expect the value should be (2,3). So what’s problem in my code?

[My Python Version is 2.7]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:54:02+00:00Added an answer on June 15, 2026 at 4:54 am

    tl;dr: multiple assignments (multiple = statements on one line) are evaluated from left-to-right, not from right-to-left (after evaluating the right-hand-side expression).

    To complicate matters, you are using tuple assignment and ‘normal’ assignment in a heady mix.

    Tuple assignment uses one assignment operator, so to swap two variables, use:

    a, b = b, a
    

    The right-hand side must evaluate to a tuple of the same number of elements as there are variables on the left-hand side. You do that, so that’s fine.

    Now, in your examples, you are not only unpacking tuples. When the left-hand side contains only one variable, the tuple is not unpacked, just simply assigned:

    a, b = 1, 2
    a = b, a
    

    becomes (2, 1).

    The fun starts when you use multiple assignments on the same line. These are processed from left to right.

    So, the following simple example:

    a = b = c = 1
    

    means that a becomes 1, then b, then c.

    Now we can understand each case:

    1. a=a,b=b,c, where a = 1, b = 2, c = 3.

      This becomes: evaluate b, c -> (2, 3), then assign that to a -> a = (2, 3). Then assign it to a, b, so a = 2, b = 3. Result: a = 2, b = 3, c = 3.

    2. b=a,b=b,c, where a = 1, b = 2, c = 3.

      Same as the case before, but now b = (2, 3) is set first, then b = 3 again, same result as case 1.

    3. c=a,b=b,c, where a = 1, b = 2, c = 3.

      Input on the right-hand side the same as cases 1. and 2., but now we first set c = (2, 3). End result as expected, a = 2, b = 3, c = (2, 3).

    4. d=a,b=b,c, where a = 1, b = 2, c = 3.

      Same as case 3. but now we set d instead. No surprises.

    What confused you here is that the assignments, after the right-hand side has been evaluated, are processed from left to right, not from right to left.

    For cases like these, it’s actually easiest to run your code (wrapped in a function), through the dis.dis() function to disassemble the python bytecode:

    >>> import dis
    >>> def f(): a=a,b=b,c
    ... 
    >>> dis.dis(f)
      1           0 LOAD_FAST                0 (b)
                  3 LOAD_GLOBAL              0 (c)
                  6 BUILD_TUPLE              2
                  9 DUP_TOP             
                 10 STORE_FAST               1 (a)
                 13 UNPACK_SEQUENCE          2
                 16 STORE_FAST               1 (a)
                 19 STORE_FAST               0 (b)
                 22 LOAD_CONST               0 (None)
                 25 RETURN_VALUE        
    

    This is the first case; note how after the BUILD_TUPLE and the DUP_TOP opcode (the latter creates an extra copy on the stack to serve the extra assignment), the first thing that happens is the STORE_FAST operation on a, followed by a UNPACK_SEQUENCE (the tuple assignment opcode), to then store the results into a and b.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, suppose I have a bunch of variables, one of them declared volatile: int
I have declared two variables (one global and other local) in a simple C
I have a project that requires the following. Four arrays will be declared in
I have declared three variables namely TemplateData tData; TaskInstance tInstance;int tID; in my program.
I have declared an enum in my server code, and would like my client
I have deployed a website using Visual Studio 2010 on IIS6. I used one
I have a two-dimensional char array declared with four strings. private static string string1
I have got ASP application, and let me clear all code is working fine
I have a module with four node types declared. My problem is, hook_load, hook_view
I have one interface that contains four functions. I have about 20 classes that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.