Swapping of two numbers in Python
In this post, you will learn different ways for swapping two numbers using the Python programming language. Such a type of question is generally asked in programming interviews, the interviewer may ask to write a program for swapping of two numbers using a particular way.
Swapping two numbers using temporary variables
In the given Python program, we are using a temporary variable temp to swap two numbers. We are storing the value of x in temp so that when the value of x is overwritten by y. This is a very simple way of swapping two variables. We are keeping a backup of the x value, which we later assign to the y.
# Python program to swap two variables
x = input('Enter first number: ')
y = input('Enter second number: ')
print("Before swaping")
print("Value of x : ", x , " value of y : ", y)
# swapping two numbers using temporary variable
temp = x
x = y
y = temp
print("After swaping")
print("Value of x : ", x , " value of y : ", y)
Output of the above code -
Enter first number: 65
Enter second number: 90
Before swaping
Value of x : 65 value of y : 90
After swaping
Value of x : 90 value of y : 65
Swapping two numbers without using swap
In the given Python program, we swap two variables without using a temporary variable.
# Python program to swap two variables
x = input('Enter first number: ')
y = input('Enter second number: ')
print("Before swaping")
print("Value of x : ", x , " value of y : ", y)
# swapping two numbers without using temporary variable
x, y = y, x
print("After swaping")
print("Value of x : ", x , " value of y : ", y)
Output of the above code -
Enter first number: 39
Enter second number: 41
Before swaping
Value of x : 39 value of y : 41
After swaping
Value of x : 41 value of y : 39
Swapping two numbers using Arithmetic Operators * & /
In the following example, we are using Arithmetic Operators for swapping of two variables.
# Python program to swap two variables
x = int(input('Enter first number: '))
y = int(input('Enter second number: '))
print("Before swaping")
print("Value of x : ", x , " value of y : ", y)
#swapping using arithmetic opertors * & /
x = x * y
y = x / y
x = x / y
#printing the output
print("After swaping")
print("Value of x : %d and value of y : %d" %(x,y))
Output of the above code -
Enter first number: 29
Enter second number: 41
Before swaping
Value of x : 29 value of y : 41
After swaping
Value of x : 41 and value of y : 29
Related Articles
Python program to multiply two numbers
Find the stop words in nltk Python
Prettytable in Python
Reverse pyramid pattern in Python
Fizzbuzz program in Python
Python split string by comma
Python alive progress bar
Python progress bar tqdm
Python language translator code
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword