johnmahugu

python - Exchanging the content of 2 variables

Jun 14th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. """
  2. Exchanging the content of 2 variables
  3.  
  4. In most languages, exchanging the content of two variable involves using a temporary variable.
  5.  
  6. In Python, this can be done with multiple assignment.
  7. """
  8. >>> a=3
  9. >>> b=7
  10. >>> (a,b)=(b,a)
  11. >>> print a
  12. 7
  13. >>> print b
  14. 3
  15.  
  16. In Python, tuples, lists and dictionnaries are your friends, really !
  17.  
  18. Highly recommended reading: Dive into Python (http://diveintopython.org/). The first chapter contains a nice tutorial on tuples, lists and dictionnaries. And don't forget to read the rest of the book (You can download the entire book for free).
  19. """
Advertisement
Add Comment
Please, Sign In to add comment