1a = 2
2b = 4
3print (a) # 2
4print (b) # 4
5# Now swap them
6# Hold one variable in c temporarily:
7c = b
8b = a
9a = c
10# Now delete variable c from memory:
11del c
12
13print (a) # 4
14print (b) # 2
1a = 10
2b = 20
3print("not swiped value of a is",a)
4print("not swiped value of b is",b)
5stored_value = a
6a = b
7b = stored_value
8print("swiped value of a is",a)
9print("swiped value of b is",b)
10
11
1a = 5
2b = 6
3# now swp the variables
4a, b = b, a
5# to swap two variables you need an other string harder than the first one
6c = a # 5
7a = b # 6
8b = c # 5
1def swap0(s1, s2):
2 assert type(s1) == list and type(s2) == list
3 tmp = s1[:]
4 s1[:] = s2
5 s2[:] = tmp
6
7# However, the easier and better way to do a swap in Python is simply:
8s1, s2 = s2, s1