In this Python Program, we will learn how to swap two variables using a temporary variable or a third variable.
Here is the source code of the program to swap two variables using a temporary variable or a third variable.
# Python Program to Swap Two Variables Using a Temporary Variable or a Third Variable
# To take inputs from the user
x = input('Enter value of x: ')
y = input('Enter value of y: ')
print('\nThe value of x before swapping: {0}'.format(x))
print('\nThe value of y before swapping: {0}'.format(y))
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('\nThe value of x after swapping: {0}'.format(x))
print('\nThe value of y after swapping: {0}'.format(y))
Enter value of x: 12
Enter value of y: 24
The value of x before swapping: 12
The value of y before swapping: 24
The value of x after swapping: 24
The value of y after swapping: 12
Comments