# Swap two variable using function in python

Write a Python Program to Exchange the Values of Two Numbers with Practical Example

Problem Description :

This program reads two integers & swaps their values using user defined function.

Program Logic:

1. Take the values of two numbers from the user using the int(input()) function and store it in two variable say num1 and num2
2. Call function swap and pass the given numbers(num1 and num2) as an argument to the swap function.
3. Create a user defined function to say swap which takes the given numbers as an argument using def keyword
4. Receive the values by the two variables x and y.
5. Assign value of x to variable temp and store it in variable temp.
6. Copy the value stored at y to x.
7. Copy the values in the variable temp to y.

8. Print the variables x and y as output and exit

Below is implementation code/Source code of program

This is a Python Program to exchange the values of two numbers using a temporary variable.

```def swap(x, y):
temp = x
x = y
y = temp
print("After swapping")
print("Value of first number :", x)
print("Value of second number :", y)
num1 = int(input("Enter first number :"))
num2 = int(input("Enter second number :"))
print("Before swapping")
print("Value of first number :",num1)
print("Value of second number :",num2)
swap(num1,num2)

```

Output:

``````Enter first number :10
Enter second number :20
Before swapping
Value of first number : 10
Value of second number : 20
After swapping
Value of first number : 20
Value of second number : 10

``````

You can also check this too

<