Swap Three Variables in Python Without Using a Temporary Variable

As a developer, I was working on a coding exercise where I had to swap three variables without using a temporary variable.

At first, it felt tricky because we’re so used to relying on an extra variable as a placeholder. But in Python, there are neat ways to handle this directly with operators or tuple unpacking.

In this tutorial, I’ll show you three simple methods I use to swap three variables without needing a temporary variable. Each method is beginner-friendly but also practical enough for real-world coding.

Method 1 – Use Tuple Unpacking

Python’s tuple unpacking is one of the simplest features. It allows you to swap multiple variables in a single line.

Here’s how I do it:

# Initial values
a, b, c = 10, 20, 30
print("Before swapping:", a, b, c)

# Swap using tuple unpacking
a, b, c = b, c, a

print("After swapping:", a, b, c)

Output:

Before swapping: 10 20 30
After swapping: 20 30 10

You can see the output in the screenshot below.

swapping of three numbers in python

This method is clean, readable, and efficient. I use it most often in my projects because it’s both efficient and elegant.

Method 2 – Use Arithmetic Operators

Another way I swap three variables is by using arithmetic operators. This method doesn’t rely on Python-specific features, so it works conceptually across many programming languages.

Here’s the code:

# Initial values
a, b, c = 5, 10, 15
print("Before swapping:", a, b, c)

# Swap using addition and subtraction
a = a + b + c   # a = 30
b = a - (b + c) # b = 5
c = a - (b + c) # c = 10
a = a - (b + c) # a = 15

print("After swapping:", a, b, c)

Output:

Before swapping: 5 10 15
After swapping: 15 5 10

You can see the output in the screenshot below.

temporary variable

This method feels like a puzzle at first, but once you understand the logic, it’s surprisingly fun to use.

Be careful with very large integers, as arithmetic operations may cause overflow in some languages. In Python, integers can grow arbitrarily large, so it’s safe here.

Method 3 – Use Multiplication and Division

This is another arithmetic trick I sometimes use. Instead of addition and subtraction, I rely on multiplication and division.

Here’s the example:

# Initial values
a, b, c = 2, 4, 8
print("Before swapping:", a, b, c)

# Swap using multiplication and division
a = a * b * c   # a = 64
b = a // (b * c) # b = 2
c = a // (b * c) # c = 4
a = a // (b * c) # a = 8

print("After swapping:", a, b, c)

Output:

Before swapping: 2 4 8
After swapping: 8 2 4

You can see the output in the screenshot below.

python swap variables without temp

This method is clever, but I don’t usually recommend it for production code because division can introduce issues if any variable is zero. Still, it’s a good trick to know.

Method 4 – Use a Rotation with Collections

Sometimes, I like to use Python’s built-in tools to make the code even simpler. The collections.deque object allows you to rotate values directly.

from collections import deque

# Initial values
a, b, c = "NY", "CA", "TX"
print("Before swapping:", a, b, c)

# Use deque rotation
d = deque([a, b, c])
d.rotate(-1)
a, b, c = d

print("After swapping:", a, b, c)

Output:

Before swapping: NY CA TX
After swapping: CA TX NY

This works great when I want to rotate a larger set of variables, not just three.

Which Method Should You Use?

  • Tuple unpacking → Best for readability and simplicity (my go-to method).
  • Arithmetic operators → Fun for interviews or when you want to avoid Python-specific features.
  • Multiplication/division → Works, but risky with zeros.
  • Deque rotation → Useful when working with more than three variables.

When I first learned about swapping variables without a temporary variable, it felt like a neat programming trick. But over the years, I’ve found that the tuple unpacking method is not just a trick; it’s something I use in real-world projects all the time.

So the next time you need to swap three variables in Python, try one of these methods. You’ll find that you don’t always need that extra placeholder variable after all.

You may also read other Python-related articles:

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.