How to swap two variables value in python?
Arunesh kumar
1 min read
For example, If you have a equal to 1 and b equal to 2 then get output a equal to 2 and b equal to 1.
First create c variable then set c equal to a, a equal to b and b equal to c.
Here is code,
a = 1
b = 2
print("OLD a = ",a)
print("OLD b = ",b)
c = a
a = b
b = c
print("a = ",a)
print("b = ",b)
Output:
OLD a = 1
OLD b = 2
a = 2
b = 1
0
Subscribe to my newsletter
Read articles from Arunesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by