Last active
May 20, 2019 07:46
-
-
Save ferminhg/c92753eef8b295cb9d7671f662aa0e04 to your computer and use it in GitHub Desktop.
Python's shorthand for in-place value swapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Why Python Is Great: | |
| # In-place value swapping | |
| # Let's say we want to swap | |
| # the values of a and b... | |
| a = 23 | |
| b = 42 | |
| # The "classic" way to do it | |
| # with a temporary variable: | |
| tmp = a | |
| a = b | |
| b = tmp | |
| # Python also lets us | |
| # use this short-hand: | |
| a, b = b, a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment