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 9272839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:57:28+00:00 2026-06-18T15:57:28+00:00

In Python, I’ve seen two variable values swapped using this syntax: left, right =

  • 0

In Python, I’ve seen two variable values swapped using this syntax:

left, right = right, left

Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually swapped?

  • 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-18T15:57:30+00:00Added an answer on June 18, 2026 at 3:57 pm

    Python evaluates expressions from left to right. Notice that while
    evaluating an assignment, the right-hand side is evaluated before the
    left-hand side.

    Python docs: Evaluation order

    That means the following for the expression a,b = b,a :

    • The right-hand side b,a is evaluated, that is to say, a tuple of two elements is created in the memory. The two elements are the objects designated by the identifiers b and a, that were existing before the instruction is encountered during the execution of the program.
    • Just after the creation of this tuple, no assignment of this tuple object has still been made, but it doesn’t matter, Python internally knows where it is.
    • Then, the left-hand side is evaluated, that is to say, the tuple is assigned to the left-hand side.
    • As the left-hand side is composed of two identifiers, the tuple is unpacked in order that the first identifier a be assigned to the first element of the tuple (which is the object that was formerly b before the swap because it had name b)
      and the second identifier b is assigned to the second element of the tuple (which is the object that was formerly a before the swap because its identifiers was a)

    This mechanism has effectively swapped the objects assigned to the identifiers a and b

    So, to answer your question: YES, it’s the standard way to swap two identifiers on two objects.
    By the way, the objects are not variables, they are objects.

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

Sidebar

Related Questions

Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python 2.5 won't let me use this syntax: try: code_that_raises_exception() except Exception as e:
Python noob so I might be going about this the wrong way I want
(Python) Given two numbers A and B. I need to find all nested groups
Python 3: Coding hangman from Invent Games with Python. Syntax msg says invalid syntax
Python's arbitrary precision decimals are lovely, but I can't seem to find a way
Python: I need to show file modification times in the 1 day ago, two
Python: I have a variable say x. I need to create a list of
Python 2.6 Using Python string.replace() seems not working for UTF-16-LE file. I think of
Python 2.7's argparse gives you two extension points where you can control how your

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.