Write a program to swap two numbers in python
Asked by admin @ in Computer Science viewed by 351 People
Write a program is python to swap two numbers.
Asked by admin @ in Computer Science viewed by 351 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 409 persons
Asked by admin @ in Computer Science viewed by 346 persons
Asked by admin @ in Physics viewed by 423 persons
Asked by admin @ in Computer Science viewed by 348 persons
Asked by admin @ in Computer Science viewed by 356 persons
Asked by admin @ in Computers and Technology viewed by 339 persons
Asked by admin @ in Computer Science viewed by 354 persons
Asked by admin @ in Computer Science viewed by 340 persons
Asked by admin @ in Computer Science viewed by 357 persons
Asked by maham237 @ in Mathematics viewed by 327 persons
Asked by admin @ in Math viewed by 388 persons
Asked by admin @ in Math viewed by 362 persons
Asked by admin @ in Mathematics viewed by 374 persons
Asked by admin @ in Computers and Technology viewed by 316 persons
Asked by admin @ in Computer Science viewed by 19024 persons
Asked by admin @ in Computer Science viewed by 15811 persons
Asked by admin @ in Computer Science viewed by 10973 persons
Asked by admin @ in Computer Science viewed by 10403 persons
Asked by admin @ in Computer Science viewed by 8137 persons
Asked by admin @ in Computer Science viewed by 7842 persons
Asked by admin @ in Computer Science viewed by 6489 persons
Asked by admin @ in Computer Science viewed by 5791 persons
Asked by admin @ in Computer Science viewed by 5693 persons
Asked by admin @ in Computer Science viewed by 5476 persons
Asked by admin @ in Computer Science viewed by 4981 persons
Asked by admin @ in Computer Science viewed by 4972 persons
Asked by admin @ in Computer Science viewed by 4684 persons
Asked by admin @ in Computer Science viewed by 4488 persons