;

Python Program to Swap Two Variables Without Using a Third Variable or a Temporary Variable


Tutorialsrack 22/04/2020 Python

In this Python Program, we will learn to swap two variables without using a third variable or a temporary variable. In this program, we can use arithmetic operations and XOR to do the same.

Here is the source code of the program to swap two variables without using a third variable or a temporary variable.

Program 1: Swap Without using Third Variable or Temporary Variable

# Python Program to Swap Two Variables Without Using a Third Variable or a Temporary Variable

x = 10
y = 20

# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')

print('\nThe value of x before swapping: {0}'.format(x))
print('\nThe value of y before swapping: {0}'.format(y))


x, y = y, x

print('\nThe value of x after swapping: {0}'.format(x))
print('\nThe value of y after swapping: {0}'.format(y))
Output

The value of x before swapping: 10
The value of y before swapping: 20

The value of x after swapping: 20
The value of y after swapping: 10

If both the Variable is Numbers then we can swap two variables using arithmetic operations. Here are some examples as follow:

Program 2: Using Addition and Subtraction
x = 10
y = 20

# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')

print('\nThe value of x before swapping: {0}'.format(x))
print('\nThe value of y before swapping: {0}'.format(y))


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

print('\nThe value of x after swapping: {0}'.format(x))
print('\nThe value of y after swapping: {0}'.format(y))
Output

The value of x before swapping: 10

The value of y before swapping: 20

 

The value of x after swapping: 20

The value of y after swapping: 10

Program 3: Using Multiplication and Division

x = 10
y = 20

# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')

print('\nThe value of x before swapping: {0}'.format(x))
print('\nThe value of y before swapping: {0}'.format(y))

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

print('\nThe value of x after swapping: {0}'.format(x))
print('\nThe value of y after swapping: {0}'.format(y))
Output

The value of x before swapping: 10

The value of y before swapping: 20

 

The value of x after swapping: 20.0

The value of y after swapping: 10.0

Or we can swap two variables without using any third variable or temporary variable and it can be done by logical operator XOR. But XOR operator works for integers only.

Program 4: Using Logical Operator - XOR

# Python Program to Swap Two Variables Without Using a Third Variable or 
# a Temporary Variable and Using a XOR Operator
 
x = 10
y = 20
 
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
 
print('\nThe value of x before swapping: {0}'.format(x))
print('\nThe value of y before swapping: {0}'.format(y))
 
x = x^y
y = x^y
x = x^y
 
print('\nThe value of x after swapping: {0}'.format(x))
print('\nThe value of y after swapping: {0}'.format(y))
Output

The value of x before swapping: 10

The value of y before swapping: 20

 

The value of x after swapping: 20

The value of y after swapping: 10


Related Posts



Comments

Recent Posts
Tags