Python Program to Swap Two Variables - GeeksforGeeks

08 Jul, 2021
Given two variables x and y, write a Python program to swap their values. Let’s see different methods in Python to do this task.  
Method 1: Using Naive approachThe most naive approach is to store the value of one variable(say x) in a temporary variable, then assigning the variable x with the value of variable y. Finally, assign the variable y with the value of the temporary variable. 
Python3
# Python program to demonstrate# swapping of two variables x = 10y = 50 # Swapping of two variables# Using third variabletemp = xx = yy = temp print("Value of x:", x)print("Value of y:", y)
Value of x: 50
Value of y: 10
Method 2: Using comma operatorUsing the comma operator the value of variables can be swapped without using a third variable. 
Python3
# Python program to demonstrate# swapping of two variables  x = 10y = 50 # Swapping of two variables# without using third variablex, y = y, x print("Value of x:", x)print("Value of y:", y)
Value of x: 50
Value of y: 10
Method 3: Using XORThe bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010). 
Python3
# Python program to demonstrate# Swapping of two variables x = 10y = 50 # Swapping using xorx = x ^ yy = x ^ yx = x ^ y print("Value of x:", x)print("Value of y:", y)
Value of x: 50
Value of y: 10
Method 4: Using arithmetic operators we can perform swapping in two ways.
Using addition and subtraction operator :
The idea is to get sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from sum. 
Python3
# Python program to demonstrate# swapping of two variables x = 10y = 50 # Swapping of two variables# using arithmetic operationsx = x + y  y = x - y x = x - y print("Value of x:", x)print("Value of y:", y)
Value of x: 50
Value of y: 10
Using multiplication and division operator :
The idea is to get multiplication of the two given numbers. The numbers can then be calculated by using the division.
Python3
# Python program to demonstrate# swapping of two variables x = 10y = 50 # Swapping of two numbers# Using multiplication operator x = x * yy = x / yx = x / y print("Value of x : ", x)print("Value of y : ", y)
Value of x :  50.0
Value of y :  10.0
Method 5: using Bitwise addition and subtraction for swapping.
Python3
#Python program to demonstrate#swapping of two numbersa = 5b = 1a = (a & b) + (a | b)b = a + (~b) + 1a = a + (~b) + 1print("a after swapping: ", a)print("b after swapping: ", b)
a after swapping:  1
b after swapping:  5
ASNR1010
singhbishalkumarsingh
python-basics
Swap-Program
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python OOPs Concepts
How to Install PIP on Windows ?
Bar Plot in Matplotlib
Defaultdict in Python
Python Classes and Objects
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
Python program to check whether a number is Prime or not