How can i return more than one value from a function to more than one variable

Simon Hayward mail at simonsblog.co.uk
Mon Dec 23 04:36:56 EST 2013


> basically what I wanna do is this :
> 
> x = 4
> y = 7
> def switch (z,w):
> ***this will switch z to w and vice verca***
>      c= z
>      z=w
>      w=c
>      print 'Now x =', w, 'and y = ' , z
>      return w
> x = switch(x,y)
> 
>  How am I supposed to do so I can  return also a value to the variable y
>  WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?
> 
> thanks in advance

Using multiple assignment.

# Swap values
x, y = 4, 7
y, x = x, y

Or to keep this in a function, return a tuple and assign from that:

def switch(x, y):
    return y, x

x, y = switch(x, y)



More information about the Python-list mailing list