Python Program to Swap Two Numbers Using Third Variable

Python program to swap two numbers using third variable can be implemented by using another variable apart from the variables which store the two given numbers. Swapping of two numbers means the exchanging of values between them.









1. Python Program & output of to Swap Two Numbers using Third Variable

Code has been copied
#*********************************************************
#               alphabetacoder.com
# Python program to swap two numbers using third variable
#*********************************************************

# take input of two numbers
num1, num2 = map(int, input("Enter two numbers = ").split())

print("Before swapping: number1 =",num1," and number2 =",num2)

#do swapping operation using third variable temp
temp = num1
num1 = num2
num2 = temp

print("After swapping: number1 =",int(num1)," and number2 =",int(num2))

Output


Enter two numbers = 5 10

Before swapping: number1 = 5 and number2 = 10

After swapping: number1 = 10 and number2 = 5