Python One Line Code To Swap Two Numbers

In python, we can swap two numbers just in one line of code. To swap is to exchange the value of variables. For example: if we have a=10 & b=20 then after swapping them their content will be a=20 & b=10. Here is the python program to swap two numbers just in one line of code.

Python Source Code: Swap Two Numbers (One Line)


firstNum = int(input('Enter first number: '))
secondNum = int(input('Enter second number: '))

firstNum, secondNum = secondNum, firstNum

print('First number = %d and second number = %d' %(firstNum, secondNum))

Output

Enter first number: 30
Enter second number: 80
First number = 80 and second number = 30