CBSE Class 11: Python Fundamentals 10

Question:13
Write a python script to take input for two numbers and interchange them (swap them)? (Method :2)(without using third variable)
Sol:

a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
print("1st value ",a," 2nd value ",b)
a=a+b
b=a-b
a=a-b
print("1st value ",a," 2nd value ",b)

Output:

Enter 1st no 10
Enter 2nd no 20
1st value  10  2nd value  20
1st value  20  2nd value  10
>>> 

Question:14
Write a python script to take input for two numbers and interchange them (swap them)? (Method :3)(Only Python can do it)
Sol:

a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
print("1st value ",a," 2nd value ",b)
a,b=b,a
print("1st value ",a," 2nd value ",b)

Output:

Enter 1st no 10
Enter 2nd no 20
1st value  10  2nd value  20
1st value  20  2nd value  10
>>>