Swap two variables (or more)
Posted: Tue Oct 06, 2015 11:26 pm
It is a common task to swap the values of two variables.
The basic idea is to use a third temporary variable like this:
temp=a
a=b
b=temp
I have just learned that there is a better way with multiple assign:
a,b=b,a
Simple, and clever
You can ofcourse use more than two variables/values.
The basic idea is to use a third temporary variable like this:
temp=a
a=b
b=temp
I have just learned that there is a better way with multiple assign:
a,b=b,a
Simple, and clever
You can ofcourse use more than two variables/values.