Write a program that prompts the user to enter number in two variables and swap the contents of the variables.(Do not use extra variable.)

Source Code

# prompt user to enter two numbers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
        
# print the original values
print("Before swapping, first number is", num1, "and second number is", num2)
        
# swap the values without using extra variable
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
        
# print the swapped values
print("After swapping, first number is", num1,"and second number is", num2)

Using tuple unpacking

# Prompt the user to enter two numbers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
  
# print the original values
print("Before swapping, first number is", num1, "and second number is", num2)
        
# Swap the values using tuple unpacking
num1, num2 = num2, num1
  
# print the swapped values
print("After swapping, first number is", num1,"and second number is", num2)

Output

Enter the first number: 10
Enter the second number: 15
Before swapping, first number is 10 and second number is 15
After swapping, first number is 15 and second number is 10