Python Code Example: Swap two values

This Python script performs a basic swapping operation between two integer variables, x and y.

Swap two values with third variable

Code Example

x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))

print("Before swapping:\nX: " + str(x) + "\nY: " + str(y))

# swapping numbers
temp = x
x = y
y = temp

print("After swapping:\nX: " + str(x) + "\nY: " + str(y))

Output

Enter a value for x: 100
Enter a value for y: 200
Before swapping:
X: 100
Y: 200
After swapping:
X: 200
Y: 100

Code Explanation

The code starts by asking the user to input two values, x and y, and converting them to integers using the int function. These values are then stored in the variables x and y respectively.

Next, the code prints the values of x and y before the swap using the print function. The str function is used to convert the values of x and y to string representations, so that they can be concatenated with the string literals in the print statement.

The code then performs the swap by creating a temporary variable temp and storing the value of x in it. Then, the value of y is assigned to x, and finally the value of temp (which is the original value of x) is assigned to y. This effectively swaps the values of x and y.

Finally, the code prints the values of x and y after the swap in the same manner as before.

This code demonstrates how to take user input, store values in variables, and perform a simple swap operation.

Swap two values without third variable

Code Example

x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))

print("Before swapping:\nX: " + str(x) + "\nY: " + str(y))

# swapping numbers
x = x + y
y = x - y
x = x - y

print("After swapping:\nX: " + str(x) + "\nY: " + str(y))

Output

Enter a value for x: 4
Enter a value for y: 8
Before swapping:
X: 4
Y: 8
After swapping:
X: 8
Y: 4

Code Explanation

The code starts by asking the user to input two values, x and y, and converting them to integers using the int function. These values are then stored in the variables x and y respectively.

Next, the code prints the values of x and y before the swap using the print function. The str function is used to convert the values of x and y to string representations, so that they can be concatenated with the string literals in the print statement.

The code then performs the swap without using a temporary variable by using a series of arithmetic operations. Here’s how it works:

  1. x = x + y – This adds the values of x and y, and stores the result in x.
  2. y = x - y – This subtracts the original value of y from the value of x (which now contains the sum of x and y), and stores the result in y.
  3. x = x - y – This subtracts the new value of y from the original value of x, and stores the result in x.

The end result of these operations is that the values of x and y have been swapped without using a temporary variable.

Finally, the code prints the values of x and y after the swap in the same manner as before.

This code demonstrates an alternative way to perform a simple swap operation without using a temporary variable.