44.8k views
2 votes
Write a python program to swap two variables.

2 Answers

2 votes
# Python swap program
x = input('Enter value of x: ')
y = input('Enter value of y: ')

# create a temporary variable and swap the values
temp = x
x = y
y = temp

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))
User Rob Van Wijk
by
5.2k points
2 votes

Program:-


\tt int(input('Enter\: the\: value\: of\:a:'))


\tt int(input('Enter\:the\:value\:of\:b:'))


\qquad\tt temp=a


\qquad\tt a=b


\qquad\tt b=temp


\tt print('The\:value\:of\:a\:after\: swapping: \{\}'\:.format(a))


\tt print('The\: value\: of\: y\:after\: swapping:\{\}'\:.format(b))

Sample Run:-


\tt a=3


\tt b=5


\tt The\:value\:of\:a\:after\:swapping:5


\tt The\:value\:of\:b\:after\:swapping:3

User Mata
by
4.6k points