Learn about Swap two numbers in Python using + and – operator in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Swap two numbers in Python using + and – operator
# Program to swap two numbers using + and – operator # input from user a = float(input('value of a: ')) b = float(input('value of b: ')) print('Values Before Swapping') print('a = ',a, 'and b = ',b) # swapping using + and - a = a + b b = a - b a = a - b # print swapped values print('Values After Swapping') print('a = ',a, 'and b = ',b)
value of a: 2 value of b: 5 Values Before Swapping a = 2.0 and b = 5.0 Values After Swapping a = 5.0 and b = 2.0
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Python Program to Calculate Compound Interest using Function