Friday, March 8, 2013

Swap Values(variables,lists) in python

In other languages:
temp = a
a = b
b = temp

In Python:
b, a = a, b
Perhaps you've seen this before. But do you know how it works?
  • The comma is the tuple constructor syntax.
  • A tuple is created on the right (tuple packing).
  • A tuple is the target on the left (tuple unpacking).
The right-hand side is unpacked into the names in the tuple on the left-hand side.
This is vald for lists aswell

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

No comments:

Post a Comment