This post contains a total of 7+ Python Program Examples with Source Code to Swap Numbers. All these programs to Swap Numbers are made using Python.
You can use the source code of these examples with credits to the original owner.
Related Posts
Python Programs to Swap Numbers
1. By Suresh B
Made by Suresh B. Program to swap 2 number without 3rd variable. Source
3 2
def myswap(a,b):
a=a+b;
b=a-b;
a=a-b;
return a,b;
x,y = myswap(2,3)
print(x,y)
2. By Rangu Dimple Bhavani
Made by Rangu Dimple Bhavani. Simple Number swapping program using Python. Source
enter a number:1 enter a number:2 ('2', '1')
def swap(inta,intb):
a=input("enter a number:\n")
b=input("enter a number:\n")
t=a
a=b
b=t
return a , b
print(swap(6,7))
3. By Indra Kumar Singh
Made by Indra Kumar Singh. Multiple ways to Swap Numbers. Source
5 10 5.0 4.0 7 6
a,b=10,5
a=a+b
b=a-b
a=a-b
print (a,b)
#Or
a,b=4,5
a=a*b
b=a/b
a=a/b
print(a)
print(b)
a,b=6,7
a,b=b,a
print(a)
print(b)
4. By Nidhi Bhati
Made by Nidhi Bhati. Basic program to swap two number. Source
20 10
num1,num2 = 10,20
temp = num1
num1 = num2
num2 = temp
print(num1,num2)
5. By Kalpesh Kalyan Patil
Made by Kalpesh Kalyan Patil. Source
a= 7 b= 5 a= 5 b= 7
a=7
b=5
print("a=",a,"b=",b)
c=a
a=b
b=c
print("a=",a,"b=",b)
6. By Aaditya jhamad
Made by Aaditya jhamad. Source
var1= 30 var2= 20
var1=20
var2=30
temp=var1
var1=var2
var2=temp
print("var1=",var1)
print("var2=",var2)
7. By Mohd Musaddiq
Made by Mohd Musaddiq. Source
Now a=5 b=7 After Swap a=7 b=5
a=5
b=7
print(f"Now\na={a}\nb={b}")
a=a^b
b=a^b
a=a^b
print(f"After Swap\na={a}\nb={b}")
8. Mohammad Adil
Made by Mohammad Adil. Number swap Program in One line. Source
6 5
a=5
b=6
a,b = b,a
print(a)
print(b)