Learn practical skills, build real-world projects, and advance your career

Programming in Python-1

Swapping

1 By making a new variable
2 without making a new variable

By making a new variable Z

#My way
intlist=[12,30]
X = (intlist[0])
Y = (intlist[1])
print ("X before swapping: {0}".format(X))
print ("Y before swapping: {0}".format(Y))
Z=X
X=Y
Y=Z
print(X)
print(Y)
X before swapping: 12 Y before swapping: 30 30 12
#Take input using input()

#input() takes input in form of the string
in_string=input()

#here extract the two numbers from the string

#we know the values of our interest are separated by comma so we can use split
mylist = in_string.split(',')
#['12','15']
#and now get our values 
#but they are in string so we first make them int
x = int(mylist[0])
y = int(mylist[1])

# SAJAN'S APPROACH

# #print x and y before swapping
# print('x before swapping: {0}'.format(x))
# print('y before swapping: {0}'.format(y))

# #Writing your swapping code here
# z = x
# x = y
# y = z
# print()
# #print x and y after swapping
# print('x after swapping: {0}'.format(x))
# print('y after swapping: {0}'.format(y))