Swap numbers without using third variable in Python
input() - Question 10
In this question, we will see how to input two integers into two variables x and y in Python programming using the input() function and swap their values without using a third variable. To know more about input() function click on the input() function lesson.
Q10) Write a program in Python to input two integers into two variables x and y and swap their values without using a third variable.
x = 5
y = 3
After Swap
x = 3
y = 5
Program
x=int(input('Enter value of x '))
y=int(input('Enter value of y '))
x=x+y
y=x-y
x=x-y
print('After Swap')
print('x={}'.format(x))
print('y={}'.format(y))
Output
Enter value of x 10 Enter value of y 20 After Swap x=20 y=10