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

  • SEARCH
  • Home
  • 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 9004653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:56:39+00:00 2026-06-16T00:56:39+00:00

Possible Duplicate: Multiple assignment in Python As we have learnt right since we started

  • 0

Possible Duplicate:
Multiple assignment in Python

As we have learnt right since we started with C that on a computer while working in one thread, all operations occur one by one.

I have a doubt in Python 3 language. I have seen codes for swapping variable values using the expression:

a,b = b,a

Or for Fibonacci series using:

a,b = b,a+b

How can these work ? But they do work :O

Does the Python system internally create some temporary variable for these ? What’s the order of assignment so that both effectively give the correct result ?

Regards,
Nikhil

  • 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-16T00:56:41+00:00Added an answer on June 16, 2026 at 12:56 am

    At a high level, you are creating two tuples, the left hand side and right hand side, and assigning the right one to the left, which changes the variables one by one to their opposites. Python is a higher level language, so there are more abstractions like this when compared to a language like C.

    At a low level, you can see quite clearly what is happening by using the dis module, which can show you the python bytecode for a function:

    >>> import dis
    >>> def test(x, y):
    ...     x, y = y, x
    ... 
    >>> dis.dis(test)
      2           0 LOAD_FAST                1 (y)
                  3 LOAD_FAST                0 (x)
                  6 ROT_TWO             
                  7 STORE_FAST               0 (x)
                 10 STORE_FAST               1 (y)
                 13 LOAD_CONST               0 (None)
                 16 RETURN_VALUE     
    

    What happens is it uses ROT_TWO to swap the order of the items on the stack, which is a very efficient way of doing this.

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

Sidebar

Related Questions

Possible Duplicate: Tuple parameter declaration and assignment oddity In Scala, one can do multiple-variable
Possible Duplicate: Multiple OHLC datasets in one image Hi So I have plotted a
Possible Duplicate: Combine Multiple child rows into one row MYSQL I have following table,
Possible Duplicate: Multiple select options in one row I have the following code but
Possible Duplicate: Python: Split string with multiple delimiters I have a program where I
Possible Duplicate: Multiple Inheritance in C# I have two classes Class A and Class
Possible Duplicate: Bind multiple events to jQuery 'live' method I have the following function:
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: How do I do multiple assignment in MATLAB? When dealing with cell
Possible Duplicate: How do I do multiple assignment in MATLAB? So let's say I

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.