Swap Two Variables in Python in a Single Line
This thing can be easily achieved in Python which in other languages usually require 3-4 lines. This is possible due to Python’s tuple data structure. So to interchange values of two variables ‘x’ and ‘y’ simply use -
x, y = y, x
“x, y” defines a new tuple which takes the values of old variables which are presented in the form of another tuple as “y, x”.
Related posts:
- Solve Windows Live Writer Proxy Problem with a Single Command
- How To Make Django Views Concise
- Removing javac not recognized error – setting PATH for javac
Article by Harsh
Harsh is an avid Blogger who blogs at Harsh Tech Talk. He writes about Blogging, Facebook, Twitter, technology and tips and tricks. By profession he is a software engineer working at Oracle, Bangalore. He is a big music freak.