Jump to content

[PYTHON] Swap Variables Value


Tupi
 Share

Recommended Posts

I. Using Native Approach

x = 10
y = 20
temp = x
x = y
y = temp
print ("Value of x:", x)
print ("Value of y:", y)

 

II. Using Comma Operator

x = 10
y = 50
x, y = y, x
print ("Value of x:", x)
print ("Value of y:", y)

 

III. Using Aritmethic Operator

x = 10
y = 50
x = x + y
y = x - y
x = x - y
print ("Value of x:", x)
print ("Value of y:", y)

 

IV. Using XOR

x = 10
y = 50
x = x^y
y = x^y
x = x^y
print ("Value of x:", x)
print ("Value of y:", y)

 

Link to comment
Share on other sites

  • Tupi locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.