---
title: "How to swap the values of two variables without using a temporary variable in Python?"  
description: "How to swap the values of two variables without using a temporary variable in Python?"  
author: "Steilla Mitchel"  
published: 2023-07-04  
updated: 2023-07-05  
canonical: https://www.mindstick.com/forum/158951/how-to-swap-the-values-of-two-variables-without-using-a-temporary-variable-in-python  
category: "python"  
tags: ["python", "programs"]  
reading_time: 2 minutes  

---

# How to swap the values of two variables without using a temporary variable in Python?

How to swap the [values of two](https://www.mindstick.com/forum/34027/how-to-swap-the-values-of-two-columns-in-sql-server) [variables](https://www.mindstick.com/articles/715/php-variables) without using a [temporary](https://www.mindstick.com/forum/2292/how-to-set-temporary-path-of-jdk-in-windows) [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Aryan Kumar

There are a few ways to swap the [values](https://www.mindstick.com/forum/327/sum-textbox-values) of [two variables](https://www.mindstick.com/forum/236/how-can-i-swap-two-variables-without-using-a-third-variable) without using a temporary variable in Python. Here are two methods:

## Method 1: Using arithmetic operators

Python

```plaintext
def swap_variables_without_temp(a, b):
    a = a + b
    b = a - b
    a = a - b
    return a, b

a = 10
b = 20

print("Before swapping:")
print("a = ", a)
print("b = ", b)

a, b = swap_variables_without_temp(a, b)

print("After swapping:")
print("a = ", a)
print("b = ", b)
```

## Method 2: Using bitwise XOR

Python

```plaintext
def swap_variables_without_temp_using_xor(a, b):
    a = a ^ b
    b = a ^ b
    a = a ^ b
    return a, b

a = 10
b = 20

print("Before swapping:")
print("a = ", a)
print("b = ", b)

a, b = swap_variables_without_temp_using_xor(a, b)

print("After swapping:")
print("a = ", a)
print("b = ", b)
```

Both methods work by using the fact that the XOR operator returns 1 when the two operands are different, and 0 when the two operands are the same. In the first method, the values of a and b are added together, and then subtracted from each other. This effectively swaps the values of a and b. In the second method, the values of a and b are XORed together, and then XORed again. This also effectively swaps the values of a and b.

Which method you use to swap the values of two variables without using a temporary variable depends on your specific needs. If you need to swap the values of two variables that are of different data types, then the first method is a good option. If you need to swap the values of two variables that are of the same data type, then the second method is a good option.


---

Original Source: https://www.mindstick.com/forum/158951/how-to-swap-the-values-of-two-variables-without-using-a-temporary-variable-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
