Open In App

Python Program to Swap Two Variables

Last Updated : 08 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. 
 

python-swap-two-variable

Method 1: Using Naive approach
The 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 = 10
y = 50
 
# Swapping of two variables
# Using third variable
temp = x
x = y
y = temp
 
print("Value of x:", x)
print("Value of y:", y)


Output

Value of x: 50
Value of y: 10

Method 2: Using comma operator
Using 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 = 10
y = 50
 
# Swapping of two variables
# without using third variable
x, y = y, x
 
print("Value of x:", x)
print("Value of y:", y)


Output

Value of x: 50
Value of y: 10

Method 3: Using XOR
The 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 = 10
y = 50
 
# Swapping using xor
x = x ^ y
y = x ^ y
x = x ^ y
 
print("Value of x:", x)
print("Value of y:", y)


Output

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 = 10
y = 50
 
# Swapping of two variables
# using arithmetic operations
x = x + y  
y = x -
x = x - y
 
print("Value of x:", x)
print("Value of y:", y)


Output

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 = 10
y = 50
 
# Swapping of two numbers
# Using multiplication operator
 
x = x * y
y = x / y
x = x / y
 
print("Value of x : ", x)
print("Value of y : ", y)


Output

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 numbers
a = 5
b = 1
a = (a & b) + (a | b)
b = a + (~b) + 1
a = a + (~b) + 1
print("a after swapping: ", a)
print("b after swapping: ", b)


Output

a after swapping:  1
b after swapping:  5


Similar Reads

Java Program to Swap two Variables
Given two numbers x and y, we need to swap their values. In this article, we will learn the Swapping of two numbers in Java. Examples of SwappingInput : x = 10, y = 20;Output : x = 20, y = 10Input : x = 213, y = 109Output : x = 109, y = 213Recommended: Please try your approach on {IDE} first, before moving on to the solution. Steps to Swap Two Numb
4 min read
Swap Two Variables in One Line
We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function?1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write "x, y = y, x".2) C/C++: Below is one generally provided classical solution: //
4 min read
Python Program to Swap Two Elements in a List
Given a list in Python and provided the positions of the elements, write a program to swap the two elements in the list. Examples: Input : List = [23, 65, 19, 90], pos1 = 1, pos2 = 3Output : [19, 65, 23, 90] Input : List = [1, 2, 3, 4, 5], pos1 = 2, pos2 = 5Output : [1, 5, 3, 4, 2] Swap Two Elements in a List using comma assignment Since the positi
4 min read
Python Program to swap two numbers without using third variable
Given two variables n1 and n2. The task is to swap the values of both the variables without using third variable.Examples: X : 10 Y : 20 After swapping X and Y, we get : X : 20 Y : 10 A : 'Hello' B : 'World' After swapping A and B, we get : A : 'World' B : 'Hello' Method 1 :- Using simple built-in method left , right = right , left This method work
4 min read
Javascript program to swap two numbers without using temporary variable
To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithmetic OperatorsUsing Bitwise XORA mixture of bitwise
6 min read
C Program to Swap two Numbers
Swapping two numbers means exchanging their values. In this article, we will learn how to swap two numbers in the C programming language. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Swap Two Numbers Using a Temporary VariableBelow is the C program to swap two numbers using a temporary variable. [GFGTABS] C //
4 min read
Python program to Swap Keys and Values in Dictionary
Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value so that they can be retrieved efficiently. Let’s discuss various ways of swapping the keys and values in Python Dictionary. Method#1 (Does not work when there are multiple same values): One naive solution maybe something like just swa
4 min read
Python Program to swap the First and the Last Character of a string
Given a String. The task is to swap the first and the last character of the string. Examples: Input: GeeksForGeeks Output: seeksForGeekG Input: Python Output: nythoP Python string is immutable which means we cannot modify it directly. But Python has string slicing which makes it very easier to perform string operations and make modifications. Follo
2 min read
Python program to Swap i'th with j'th elements in List
Given a list, the task is to write a Python program to the given list of elements, toggle every i and j elements in the list. Examples: Input : test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4], i, j = 4, 8 Output : [8, 7, 4, 0, 4, 8, 2, 9, 8, 4, 8] Explanation : 4 is swapped by 8 at each occurrence. Input : test_list = [4, 7, 8, 0, 8, 4], i, j = 4, 8
4 min read
Python Program to Swap dictionary item's position
Given a Dictionary, the task is to write a python program to swap positions of dictionary items. The code given below takes two indices and swap values at those indices. Input : test_dict = {'Gfg' : 4, 'is' : 1, 'best' : 8, 'for' : 10, 'geeks' : 9}, i, j = 1, 3 Output : {'Gfg': 4, 'for': 10, 'best': 8, 'is': 1, 'geeks': 9} Explanation : (for : 10)
4 min read
Practice Tags :