Swapping of two variable in single line in python by R4R Team

What is swapping ?

Exchange the value of the two variable which is already exist is called swapping.

Example -

Before swapping
a=10
b=20
After swapping
a=20
b=10

program-

print("Enter first number")
a=input()
print("Enter second number")
b=input()
print("Before swapping -")
print(a,b)
a,b=b,a
print("After swapping -")
print(a,b)

output -

Enter first number
3
Enter second number
4
Before swapping -
3 4
After swapping -
4 3

Enter first number
@
Enter second number
#
Before swapping -
@ #
After swapping -
# @


- In this program, we swap two variable with only one line i.e. a,b=b,a and this type of operation is not supported by other programming language like C, C++, JAVA.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!