-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathswap.py
More file actions
36 lines (27 loc) · 765 Bytes
/
swap.py
File metadata and controls
36 lines (27 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
print("Enter two values to swap")
a = int(input())
b = int(input())
# using a temporary variable
print("Swapping using temporary variable...")
t = a
a = b
b = t
print("Value of a is {a} and\nValue of b is {b}".format(a=a, b=b))
# using XOR operator, eliminating the need for 3rd variable
print("Swapping using XOR operator...")
a = a ^ b
b = a ^ b
a = a ^ b
print("Value of a is {a} and\nValue of b is {b}".format(a=a, b=b))
def swap_func():
'''swap the contents of a and b
@author kgashok
@param a is global
@param b is global
@returns None
'''
global a, b
print("swap_func function called")
a, b = b, a
swap_func() # go back to their original values
print("Value of a is {a} and\nValue of b is {b}".format(a=a, b=b))