Python Code Example: Swap two Values

In this example, we will examine a Python script that demonstrates the process of swapping the values of two variables. The script prompts the user to input two integers, displays their values before and after the swap, and uses a temporary variable to facilitate the swapping. This example highlights the use of basic input/output functions, variable assignment, and string formatting in Python.

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))

Detailed Code Explanation

Input Statements

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

These lines prompt the user to enter two numbers, which will be stored in the variables x and y.

Step-by-Step Explanation

• input("Enter a value for x: "): Displays the message “Enter a value for x:” and waits for the user to input a value.
• int(...): Converts the user input (a string) to an integer.
• The same process is repeated for the variable y.

Initial Output Statement

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

This line prints the values of x and y before the swap.

Step-by-Step Explanation

• str(x): Converts the integer value of x to a string.
• "Before swapping:\nX: " + str(x) + "\nY: " + str(y): Constructs a string that includes the values of x and y separated by a newline character.
• print(...): Outputs the constructed string to the console.

Swapping Operation

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

These lines swap the values of x and y using a temporary variable temp.

Step-by-Step Explanation

• temp = x: Stores the value of x in the temporary variable temp.
• x = y: Assigns the value of y to x.
• y = temp: Assigns the value stored in temp (original value of x) to y.

Final Output Statement

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

This line prints the values of x and y after the swap.

Step-by-Step Explanation

• str(x): Converts the integer value of x to a string.
• "After swapping:\nX: " + str(x) + "\nY: " + str(y): Constructs a string that includes the new values of x and y separated by a newline character.
• print(...): Outputs the constructed string to the console.

In the next code example, we will examine a Python script that demonstrates the process of swapping the values of two variables without using a temporary variable. The script prompts the user to input two integers, displays their values before and after the swap, and uses arithmetic operations to facilitate the swapping. This example highlights the use of basic input/output functions, arithmetic operations, and string formatting in Python.

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))

Detailed Code Explanation

Initial Output Statement

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

This line prints the values of x and y before the swap.

Step-by-Step Explanation

• str(x): Converts the integer value of x to a string.
• "Before swapping:\nX: " + str(x) + "\nY: " + str(y): Constructs a string that includes the values of x and y separated by a newline character.
• print(...): Outputs the constructed string to the console.

Swapping Operation

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

These lines swap the values of x and y using arithmetic operations without a temporary variable.

Step-by-Step Explanation

• x = x + y: Adds the values of x and y and stores the result in x. At this point, x holds the sum of the original values of x and y.
• y = x - y: Subtracts the original value of y from the current value of x (which is x + y). This operation effectively assigns the original value of x to y.
• x = x - y: Subtracts the new value of y (which is the original value of x) from the current value of x (which is x + y). This operation effectively assigns the original value of y to x.

Final Output Statement

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

This line prints the values of x and y after the swap.

Step-by-Step Explanation

• str(x): Converts the integer value of x to a string.
• "After swapping:\nX: " + str(x) + "\nY: " + str(y): Constructs a string that includes the new values of x and y separated by a newline character.
• print(...): Outputs the constructed string to the console.