Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the logic for x,y=y,x to swap the values? [duplicate]

Tags:

python

Code:

x="salil"
y="Ajay"
y,x=x,y

print x +" "+ y

Output:

Ajay salil

What is a logical explanation for this?

like image 235
salil vishnu Kapur Avatar asked Jul 13 '15 02:07

salil vishnu Kapur


People also ask

How XOR is used to swap two numbers?

In computer programming, the exclusive or swap (sometimes shortened to XOR swap) is an algorithm that uses the exclusive or bitwise operation to swap the values of two variables without using the temporary variable which is normally required.

How do you swap variable values?

If the two variables are numbers, their values can be swapped using arithmetic operators such as addition and subtraction ( + , - ) or multiplication and division ( * , / ). This swapping method is based on calculating the sum of the two numbers and then swapping them using the sum and the difference from the sum.


1 Answers

The way this mechanism works is a combination of two features -- forming implicit tuples, and tuple/list unpacking.

When you do something = x, y, what Python will do is implicitly create a tuple (a sort of immutable list) comprising of the two elements, x and y. So, the following two lines of code are exactly equivalent:

something = x, y
something = (x, y)

A tuple can, of course, contain more than two elements:

something = a, b, c, d, e
something = (a, b, c, d, e)

The intended use case of this feature is to make it easier/cleaner to do things like return multiple values from a function:

def foo():
    return "hello", "world"

The second feature is tuple/list unpacking. Whenever you have a series of variables on the left-hand side, and any sort of list, tuple, or other collection on the other, Python will attempt to match up each of the elements in the right to the ones on the left:

>>> a, b, c = [11, 22, 33]
>>> print(a)
11
>>> print(b)
22
>>> print(c)
33

If it helps, the line a, b, c = [11, 22, 33] is basically identical to doing:

temp = [11, 22, 33]
a = temp[0]
b = temp[1]
c = temp[2]

Note that the right-hand side can be literally any kind of collection, not just tuples or lists. So the following code is valid:

>>> p, q = "az"
>>> print(p + "  " + q)
a  z
>>>
>>> s, t = {'cat': 'foo', 'dog': 'bar'}
>>> print(s + "  " + t)
cat  dog

(Though, since dictionaries in Python are not obligated to be in any particular order, and since the order of the keys can be freely scrambled, unpacking them probably isn't going to be useful since you'd potentially get different results each time.)

If the number of variables and the number of elements in the collection do not match up, Python will throw an exception:

>>> a, b = (1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

>>> a, b, c = (1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

So that means that if we called our foo function from above and did the following:

>>> x, y = foo()

...the variable x would equal the string "hello", and the variable y would equal the string "world".


So ultimately, that means that your original snippit of code:

x = "salil"
y = "Ajay"
y, x = x, y

...is logically equivalent to the following:

x = "salil"
y = "Ajay"
temp = (x, y)  # evaluates to ("salil", "Ajay")
y, x = temp

...which broken down even more, is logically equivalent to the following:

x = "salil"
y = "Ajay"
temp = (x, y)  # evaluates to ("salil", "Ajay")
y = temp[0]
x = temp[1]

Note that you can think of these two operations take place separately. First the tuple is formed and evaluated, then the tuple is unpacked back into the variables. The net effect is that the values of your two variables are interchanged.

(However, as it turns out, the CPython interpreter (the original and 'standard' implementation of Python) does a bit of optimization here: it will optimize the swap and won't do the full tuple unpacking -- see comments below. I'm not sure if other implementations of Python do the same, though I suspect they might. In any case, this sort of optimization is implementation-specific, and is independent to the design of the Python language itself.)

like image 166
Michael0x2a Avatar answered Sep 29 '22 13:09

Michael0x2a