def swap_value(x,y):
temp = x
x = y
y = temp
return
swap_value(3,4)
I tried it like this. But it didn't work
Your function is not returning anything, so maybe you want to return a tuple like:
def swap_value(x, y):
temp = x
x = y
y = temp
return x, y
Wich is a more verbose way of just doing:
def swap_value(x, y):
return y, x
Problem with the above methods is you're not doing a proper swapping, test below:
x, y = 3, 4
print(swap_value(x, y))
print(x, y)
The most pythonic way to swap values in python would be using a simple oneliner, this would be the minimal pythonic way to swap values, no need to wrap swapping into a function (unless the swap did something else):
x, y = 3, 4
x, y = y, x
print(x, y)