Skip to content
Home » Swap two numbers in Python using * and / operator

Swap two numbers in Python using * and / operator

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 of the numbers
a = a * b
b = a / b
a = a / b

# print swapped values
print('Values After Swapping')
print('a = ',a, 'and b = ',b)

Output:

value of a: 6
value of b: 7
Values Before Swapping
a =  6.0 and b =  7.0
Values After Swapping
a =  7.0 and b =  6.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 : Swap two numbers in Python using + and – operator