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.
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))
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
.
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.y
.print("Before swapping:\nX: " + str(x) + "\nY: " + str(y))
This line prints the values of x
and y
before the swap.
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 numbers
temp = x
x = y
y = temp
These lines swap the values of x
and y
using a temporary variable temp
.
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
.print("After swapping:\nX: " + str(x) + "\nY: " + str(y))
This line prints the values of x
and y
after the swap.
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.
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))
print("Before swapping:\nX: " + str(x) + "\nY: " + str(y))
This line prints the values of x
and y
before the swap.
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 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.
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
.print("After swapping:\nX: " + str(x) + "\nY: " + str(y))
This line prints the values of x
and y
after the swap.
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.