Write a program to swap two numbers in python
Asked by admin @ in Computer Science viewed by 343 People
Write a program is python to swap two numbers.
Asked by admin @ in Computer Science viewed by 343 People
Write a program is python to swap two numbers.
Answered by admin @
Answer:
Source Code: Using a temporary variable
# Python program to swap two variables
x = 5
y = 10
# To take inputs from the user
#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))
Output
The value of x after swapping: 10
The value of y after swapping: 5
In this program, we use the temp variable to hold the value of x temporarily. We then put the value of y in x and later temp in y. In this way, the values get exchanged.
Source Code: Without Using Temporary Variable
In Python, there is a simple construct to swap variables. The following code does the same as above but without the use of any temporary variable.
x = 5
y = 10
x, y = y, x
print("x =", x)
print("y =", y)
If the variables are both numbers, we can use arithmetic operations to do the same. It might not look intuitive at first sight. But if you think about it, it is pretty easy to figure it out. Here are a few examples
Addition and Subtraction
x = x + y
y = x - y
x = x - y
Multiplication and Division
x = x * y
y = x / y
x = x / y
XOR swap
This algorithm works for integers only
x = x ^ y
y = x ^ y
x = x ^ y
Asked by admin @ in Computer Science viewed by 398 persons
Asked by admin @ in Computer Science viewed by 336 persons
Asked by admin @ in Physics viewed by 413 persons
Asked by admin @ in Computer Science viewed by 340 persons
Asked by admin @ in Computer Science viewed by 350 persons
Asked by admin @ in Computers and Technology viewed by 332 persons
Asked by admin @ in Computer Science viewed by 345 persons
Asked by admin @ in Computer Science viewed by 332 persons
Asked by admin @ in Computer Science viewed by 351 persons
Asked by maham237 @ in Mathematics viewed by 319 persons
Asked by admin @ in Math viewed by 378 persons
Asked by admin @ in Math viewed by 357 persons
Asked by admin @ in Mathematics viewed by 364 persons
Asked by admin @ in Computers and Technology viewed by 308 persons
Asked by admin @ in Computer Science viewed by 19012 persons
Asked by admin @ in Computer Science viewed by 15788 persons
Asked by admin @ in Computer Science viewed by 10957 persons
Asked by admin @ in Computer Science viewed by 10392 persons
Asked by admin @ in Computer Science viewed by 8124 persons
Asked by admin @ in Computer Science viewed by 7833 persons
Asked by admin @ in Computer Science viewed by 6475 persons
Asked by admin @ in Computer Science viewed by 5781 persons
Asked by admin @ in Computer Science viewed by 5680 persons
Asked by admin @ in Computer Science viewed by 5467 persons
Asked by admin @ in Computer Science viewed by 4973 persons
Asked by admin @ in Computer Science viewed by 4963 persons
Asked by admin @ in Computer Science viewed by 4677 persons
Asked by admin @ in Computer Science viewed by 4480 persons