Python program for swapping the value of two integers without third variable

Learn, how to swap the value of two integers without using third variable in Python program? By Anuj Singh Last updated : January 04, 2024

Problem statement

Given two numbers, write a Python program to swap them without using third variable.

1. Swapping by multiple assignments using tuple

To swap two variables without using a third variable, you can use multiple assignments using tuples.

Syntax

(x, y) = (y, x)

Python code to swap two variables without using a third variable by multiple assignments tuple

# Code to swap two variables

# Input the numbers
x = int(input("ENTER THE VALUE OF X: "))
y = int(input("ENTER THE VALUE OF Y: "))

# Printing numbers before swapping
print("Before swapping:")
print("X :", x, " Y :", y)

# Swapping the numbers
(x, y) = (y, x)

# Printing numbers after swapping
print("After swapping:")
print("X :", x, " Y :", y)

Output

The output of the above example is:

ENTER THE VALUE OF X: 10
ENTER THE VALUE OF Y: 20
Before swapping:
X : 10  Y : 20
After swapping:
X : 20  Y : 10

2. Swapping by XORing variables

For swapping two variables (if they are integers), you can use the XOR (^) operator.

Syntax

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

Python code to swap two variables without using a third variable by XORing variables

# Code to swap two variables

# Input the numbers
x = int(input("ENTER THE VALUE OF X: "))
y = int(input("ENTER THE VALUE OF Y: "))

# Printing numbers before swapping
print("Before swapping:")
print("X :", x, " Y :", y)

# Swapping the numbers
x ^= y
y ^= x
x ^= y

# Printing numbers after swapping
print("After swapping:")
print("X :", x, " Y :", y)

Output

The output of the above example is:

ENTER THE VALUE OF X: 10
ENTER THE VALUE OF Y: 20
Before swapping:
X : 10  Y : 20
After swapping:
X : 20  Y : 10

3. Swapping by multiplying & dividing variables

You can also perform the multiplication and division operation with these variables to get swapped values.

Syntax

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

Python code to swap two variables without using a third variable by multiplying & dividing variables

# Code to swap two variables

# Input the numbers
x = int(input("ENTER THE VALUE OF X: "))
y = int(input("ENTER THE VALUE OF Y: "))

# Printing numbers before swapping
print("Before swapping:")
print("X :", x, " Y :", y)

# Swapping the numbers
x = x * y
y = x / y
x = x / y

# Printing numbers after swapping
print("After swapping:")
print("X :", x, " Y :", y)

Output

The output of the above example is:

ENTER THE VALUE OF X: 10
ENTER THE VALUE OF Y: 20
Before swapping:
X : 10  Y : 20
After swapping:
X : 20.0  Y : 10.0

4. Swapping by adding & subtracting variables

You can also perform the addition and subtracting operation with these variables to get swapped values.

Syntax

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

Python code to swap two variables without using a third variable by adding & subtracting variables

# Code to swap two variables

# Input the numbers
x = int(input("ENTER THE VALUE OF X: "))
y = int(input("ENTER THE VALUE OF Y: "))

# Printing numbers before swapping
print("Before swapping:")
print("X :", x, " Y :", y)

# Swapping the numbers
x = x + y 
y = x - y
x = x - y

# Printing numbers after swapping
print("After swapping:")
print("X :", x, " Y :", y)

Output

The output of the above example is:

ENTER THE VALUE OF X: 10
ENTER THE VALUE OF Y: 20
Before swapping:
X : 10  Y : 20
After swapping:
X : 20  Y : 10

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.