# Python Program to Swap Two Numbers

To understand this example, you should have knowledge of following Python programming topics:

There are two ways to swap two numbers:

• Without Using Temporary Variable
• Using Temporary Variable

## Source Code: Swap Using a temporary variable

```# Python Program to Swap Two Variables Using Temporary Variable

x = 5
y = 7

print("Before Swapping: ")
print("Value of x = ", x)
print("Value of y = ", y)

temp = x
x = y
y = temp
print("After Swapping: ")
print("Value of x = ", x)
print("Value of y = ", y)```

It’s also available on GitHub – https://github.com/studygyaan/python-tutorial/blob/master/Python-Swap-Temp.py

## Source Code: Swap Without Using Temporary Variable

```# Example  Addition and Subtraction
x = x + y
y = x - y
x = x - y

# Example  Multiplication and Division
x = x * y
y = x / y
x = x / y```
```# Python Program to Swap Two Variables without Using Temporary Variable

x = 5
y = 7

print("Before Swapping: ")
print("Value of x = ", x)
print("Value of y = ", y)

x = x + y
y = x - y
x = x - y

print("After Swapping: ")
print("Value of x = ", x)
print("Value of y = ", y)```