Ask Your Question
3

Can we switch back and forth between two values in Python?

asked 2023-07-13 11:13:08 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-07-13 11:21:01 +0000

qstack gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-13 11:13:08 +0000

Seen: 10 times

Last updated: Jul 13 '23