Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, we can switch back and forth between two values using a temporary variable in Python. Here is an example:

a = 5
b = 10

temp = a
a = b
b = temp

print("a = ", a)  # Output: a = 10
print("b = ", b)  # Output: b = 5

In this example, we have two variables a and b with values 5 and 10 respectively. We create a temporary variable temp to store the value of a. We then assign the value of b to a and the value of temp (which is the original value of a) to b. This switches the values of a and b. The output shows that a is now 10 and b is now 5.