Dremendo Tag Line

Swap numbers using third variable in Python

input() - Question 9

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 using a third variable z. To know more about input() function click on the input() function lesson.

Q9) Write a program in Python to input two integers into two variables x and y and swap their values using a third variable z.

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 '))
z=x
x=y
y=z
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
video-poster