a = y
b = x
x = 3
y = 5
# prints "This is before the swap: x = 3; y = 5."
print "This is before the swap: x = %d; y = %d." % (x,y)
x,y = swap(x,y)
# prints "This is after the swap: x = 5; y = 3."
print "This is after the swap: x = %d; y = %d." % (x,y)
I'm a newer programmer, so this may not be the most efficient way.
Basically what happens is since you give two variables to assign the fuction to, the return values go into those respective variables. After that line is executed, the function's "a" will be the new "x" and the function's "b" will be the new "y".
Here is a case where you don't know exactly how many arguments you will be passing to a Python function:
# explore the argument tuple designated by *args
# used for cases where you don't know the number of arguments
size = len(args)
sum = 0
for k in args:
sum += k
# return a tuple of three arguments
return args, sum, sum/float(size)
print sum_average(2, 5, 6, 7) # ((2, 5, 6, 7), 20, 5.0)
# or unpack into a tuple of appropriate variables
tuple, sum, average = sum_average(2, 5, 6, 7)
print "sum of %s = %d" % (tuple, sum) # sum of (2, 5, 6, 7) = 20
print "average of %s = %0.2f" % (tuple, average) # average of (2, 5, 6, 7) = 5.00
# or just pick one return value, here value at index 1 = the sum
print "sum =", sum_average(2, 5, 6, 7) # sum = 20
One thing that bothers me, vegaseat used tuple for a variable name, isn't that a keyword in Python and therefore off limits!?
Well. If you print it, you get "<type 'tuple'>." You get a value, but technically it's not a keyword such as "if" or "for." It is possible to assign a value to "tuple" and use it for your program, but it would be better to use a different name. Plus, there's probably a better variable to explain what you're using.