Swapping of two numbers in Python
In this post, you will learn how to swap two numbers using Python programming language.
Here, we have mentioned three ways to swap two numbers. The program uses a temporary variable, while the second and third program do not use any temporary variable.
Python Swap two numbers using temporary variable
In the given Python program, we use a temporary variable, 'temp' to swap two numbers. First, we assign the value of 'x' to 'temp'. Then, assign the value of 'y' to 'x' and the value of 'temp' to 'y'. Finally, the value of 'x' will be assigned to 'y' and the value of 'y' will be assigned to 'x'.
# Python program to swap two variables
# Taking user inputs
x = int(input("Enter the value of x: "))
y = int(input("Enter the value of y: "))
print("Before swapping: %d %d\n" %(x,y))
# creating a temporary variable
# and swapping the numbers
temp = x
x = y
y = temp
print("\nAfter swapping: %d %d\n"%(x,y))
Output of the above code:
Enter the value of x: 320
Enter the value of y: 432
Before swapping: 320 432
After swapping: 432 320
Python Swap two numbers without using temporary variable
In the given Python program, we ask the user to enter two integer values and store them in two variables. Then, we swap the entered value using the addition and subtraction operators.
x = int(input("Enter the value of x: "))
y = int(input("Enter the value of y: "))
# Displaying values
print("Before swapping: %d %d\n" %(x,y))
# swapping numbers
x = x + y
y = x - y
x = x - y
# Verifing swapped values
print("\nAfter swapping: %d %d\n"%(x,y))
Output of the above code:
Enter the value of x: 200
Enter the value of y: 160
Before swapping: 200 160
After swapping: 160 200
Python Swap two numbers using Multiplication and division
In the given Python program, we swap two numbers using the arithmetic operators multiplication and division.
x = int(input("Enter the value of x: "))
y = int(input("Enter the value of y: "))
# Displaying values
print("Before swapping: %d %d\n" %(x,y))
# swapping 'x' and 'y' using
# multiplication and division
x = x * y;
y = x / y;
x = x / y;
# Verifing swapped values
print("\nAfter swapping: %d %d\n"%(x,y))
Output of the above code:
Enter the value of x: 320
Enter the value of y: 220
Before swapping: 320 220
After swapping: 220 320
Python Swap two numbers using bitwise XOR operator
Here, we have used the bitwise XOR operator to swap two variables using Python programming. The XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ.
x = 55
y = 45
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
# Swapping code
x ^= y
y ^= x
x ^= y
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)
Output of the above code:
Before swapping:
Value of x : 55 and y : 45
After swapping:
Value of x : 45 and y : 55
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary