How to Swap Two Numbers in Python

When I started working with Python more than 10 years ago, one of the first things I learned was how to swap two numbers.

For example, whether I was working on sorting algorithms, data manipulation, or even financial models for US-based clients, swapping values came up again and again.

In Python, there are several ways to swap two numbers. Some methods are extremely simple, while others are more mathematical or even a little “hacky.”

In this tutorial, I’ll walk you through five practical methods to swap two numbers in Python. I’ll also share my personal experience on when to use each method.

Method 1 – Use Python Tuple Unpacking

The most Pythonic way to swap numbers is by using tuple unpacking. This method is short, clean, and works across all versions of Python.

x = 10
y = 25

print("Before Swap: x =", x, "y =", y)

x, y = y, x

print("After Swap: x =", x, "y =", y)

You can refer to the screenshot below to see the output.

swapping of two numbers in python

When I run this code, Python instantly swaps the values of x and y. This method is my go-to because it’s simple and doesn’t require extra steps.

Method 2 – Use Python Temporary Variable

Sometimes, I like to show beginners the temporary variable method because it’s easy to understand. Even though it’s not the most efficient way, it’s still useful in interviews or when explaining logic.

x = 45
y = 90

print("Before Swap: x =", x, "y =", y)

temp = x
x = y
y = temp

print("After Swap: x =", x, "y =", y)

You can refer to the screenshot below to see the output.

swap two numbers in python

Here, I use a third variable temp to hold the value of x. Then I assign y to x and finally temp to y. This method is more traditional, but it’s still reliable in Python.

Method 3 – Use Python Arithmetic Operations

When I was working on a financial model for a client in New York, I used arithmetic operations to swap numbers without extra memory.

This method uses addition and subtraction.

x = 100
y = 200

print("Before Swap: x =", x, "y =", y)

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

print("After Swap: x =", x, "y =", y)

You can refer to the screenshot below to see the output.

python program to swap two numbers

This works because we temporarily store the sum in x. Then we adjust y and x step by step. It’s clever, but you need to be careful with very large numbers, since it can cause overflow in some programming languages (not usually in Python).

Method 4 – Use Multiplication and Division

Another mathematical trick is using multiplication and division. I don’t use this often, but it’s good to know in case you want to avoid extra variables.

x = 15
y = 30

print("Before Swap: x =", x, "y =", y)

x = x * y
y = x // y
x = x // y

print("After Swap: x =", x, "y =", y)

You can refer to the screenshot below to see the output.

how to swap two numbers in python

Here, I use integer division (//) to make sure the result is always an integer. This method is fun to try, but I recommend sticking to tuple unpacking for real-world Python projects.

Method 5 – Swap Two Numbers Using Bitwise XOR

One of the most interesting methods I discovered early in my career was using bitwise XOR. This method is often discussed in coding interviews because it shows a deep understanding of how computers handle binary data.

x = 7
y = 12

print("Before Swap: x =", x, "y =", y)

x = x ^ y
y = x ^ y
x = x ^ y

print("After Swap: x =", x, "y =", y)

This works because XOR has a unique property: if you XOR the same number twice, you get back the original number. While it’s not the most Pythonic option, it’s still a great technique to know.

Which Method Should You Use?

After years of writing Python code, here’s my advice:

  • Use Tuple Unpacking → It’s clean, fast, and Pythonic.
  • Use Temporary Variable → Best for teaching beginners.
  • Use Arithmetic or Multiplication/Division → Fun tricks, but not practical for real-world Python code.
  • Use XOR → Great for interviews or low-level programming discussions.

In real-life projects, I always go with tuple unpacking because it’s the most readable and maintainable.

Swapping two numbers in Python may seem like a beginner’s exercise, but it’s a powerful concept that shows how flexible the language is.

Whether you use tuple unpacking, arithmetic operations, or even XOR, each method teaches you something new about Python.

If you’re just starting, stick with tuple unpacking. But if you’re preparing for coding interviews or want to explore Python tricks, try the other methods too.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.