Python Program to swap two numbers using the function


In this program, You will learn how to swap two numbers using the function in Python.


10 20 => 20 10

33 44 => 44 33

Example: How to swap two numbers using the function in Python.

def swap(x, y):
    temp = x

    x = y
    y = temp

    print("After swap x is:", x)
    print("After swap y is:", y)


x = int(input("Enter first number:"))
y = int(input("Enter second number:"))

swap(x, y)

Output:

Enter first number:10
Enter second number:20
After swap x is: 20
After swap y is: 10