Wednesday, September 30, 2015

Swap with python

Lets start feelPython journey with one of the simplest algorithms ever. Swapping.

Usually we swap two variables like this.

>>> a = 5
>>> b = 6
>>> temp = a
>>> a = b
>>> b = temp
>>> print a, b
6 5


But python makes swap easier

>>> a = 5
>>> b = 6
>>> a, b = b, a
>>> print a, b
6 5


This feature is called unpacking.