Swapping Numbers in Python

22 Jul 2024
Beginner
156 Views

How to Swap Two Numbers in Python?

Swapping two numbers means exchanging their values. You might be familiar with the term, "swapping". You might even have performed swapping of numbers while learning C, C++, or Java. Swapping is used in sorting algorithms, mathematical calculations, memory management, and various programming tasks.

Well, do you know how to swap two numbers in Python? Have you ever tried swapping numbers using various techniques in Python? In this Python tutorial, we'll study how to swap two numbers in a Python program.

But, before proceeding refer to these:

Ways to Swap Two Numbers in Python

1. Using a Temporary Variable

In this approach, we store one of the variables' values in a temporary variable. The value of the other variable is then stored in the first variable. At last, the value of the temporary variable is stored in the second variable.

Program to Swap Two Numbers in Python Using a Temporary Variable

x = 100
y = 507

temp = x
x = y
y = temp

print("Value of x:", x)
print("Value of y:", y)

We have two numbers inside the variables x and y. We have used the temporary variable, temp to store the value of x.

Value of x: 507
Value of y: 100

2. Using Comma Operator

This is the most simple method. We use the comma "," operator to swap two numbers.

Program to Swap Two Numbers in Python Using Comma Operator

x = 790
y = 353

x, y = y, x

print("Value of x:", x)
print("Value of y:", y)

The variables x and y are swapped using the "," operator.

Value of x: 353
Value of y: 790

3. Using Arithmetic Operators

1. Using Addition and Subtraction Operator

Here, we add two numbers and store the sum in one of the two given numbers. The numbers can then be swapped using the sum and the result of subtraction from the sum.

Program to Swap Two Numbers in Python Using Addition and Subtraction

x = 900
y = 530

# Swapping of two variables using arithmetic operations
x = x + y
y = x - y
x = x - y

print("Value of x:", x)
print("Value of y:", y)

The variables x and y are added and stored in the variable x. The value in y is subtracted from the sum in x and stored in y. Again the new values of x and y are subtracted from each other and stored in x.

Value of x: 530
Value of y: 900

2. Using Multiplication and Subtraction Operator

Here, we multiply two numbers and store their product in one of the two given numbers. The division is then performed and swapping takes place.

Program to Swap Two Numbers in Python Using Multiplication and Division

x = 108
y = 289

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

print("Value of x: ", x)
print("Value of y: ", y)

The variables x and y are multiplied and stored in the variable x. The product value in x is divided by y and stored in y. Again the new values of x and y are divided and stored in x.

Output

Value of x: 289.0
Value of y: 108.0
 Read More: Arithmetic Operators in Python

4. Using Bitwise Operators

Program to Swap Two Numbers in Python Using Bitwise Addition and Subtraction

x = 50
y = 102
x = (x & y) + (x | y)
y = x + (~y) + 1
x = x + (~y) + 1
print("x after swapping: ", x)
print("y after swapping: ", y)

We've used Bitwise AND(&), Bitwise OR(|), and Bitwise NOT(~) operators to perform swapping of x and y.

Output

x after swapping:  102
y after swapping:  50

2. Using Bitwise XOR

We perform the XOR operation three times on the two variables. Then we can swap their values without using a third variable.

Program to Swap Two Numbers in Python Using Bitwise XOR

x = 109
y = 507

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

print("Value of x:", x)
print("Value of y:", y)

Output

Value of x: 507
Value of y: 109
 Read More: Bitwise Operators in Python

5. Using User-Defined Function

We can swap two variables using a function in Python. We can pass the two numbers as arguments to the functions and return or print the swapped data.

We can do this in four different ways:

1. With arguments and return value

def swap(x, y):
print("Before swapping a: ", x)
print("Before swapping b: ", y)
x, y = y, x
return x, y

a = 87
b = 94
a, b = swap(a, b)
print("After swapping a becomes: ", a)
print("After swapping b becomes: ", b)

Output

Before swapping a:  87
Before swapping b:  94
After swapping a becomes:  94
After swapping b becomes:  87

2. With arguments and without return value

def swap(x, y):
print("Before swapping a: ", x)
print("Before swapping b: ", y)

x, y = y, x
print("After swapping a becomes: ", x)
print("After swapping b becomes: ", y)

a = 98
b = 67

swap(a, b)

Output

Before swapping a:  98
Before swapping b:  67
After swapping a becomes:  67
After swapping b becomes:  98

3. Without arguments and with return value

def swap():
x = 76
y = 89
print("Before swapping a: ", x)
print("Before swapping b: ", y)

x, y = y, x
return x, y

a, b = swap()
print("After swapping a becomes:", a)
print("After swapping b becomes:", b)

Output

Before swapping a:  76
Before swapping b:  89
After swapping a becomes: 89
After swapping b becomes: 76

4. Without arguments and return value

def swap():
x = 87
y = 43
print("Before swapping a: ", x)
print("Before swapping b: ", y)

x, y = y, x
print("After swapping a becomes: ", x)
print("After swapping b becomes: ", y)

swap()

Output

Before swapping a:  87
Before swapping b:  43
After swapping a becomes:  43
After swapping b becomes:  87
Summary

Python has above all methods that make number switching easy. These methods show how Python can perform simple operations with little syntax and simplify a lot of typical programming jobs. I hope that you have understood all the mentioned methods.

FAQs

Q1. How do you rearrange numbers in Python?

You can sort a list in Python using the sort() method. The method accepts two optional arguments: reverse : which sorts the list in the reverse order (descending) if True or in the regular order (ascending) if False (which it is by default) key : a function you provide to describe the sorting method.

Q2. How do you shift numbers in Python?

The left shift operator in python can be represented by this symbol << .

Q3. How to sort 5 numbers in Python?

In the above example, the sort() method sorts the numbers list in ascending order. The result will be [1, 2, 3, 4, 5].
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
 Azure Developer Certification Training Sep 14 SAT, SUN Filling Fast10:00AM to 12:00PM (IST) ASP.NET Core Certification Training Sep 15 SAT, SUN Filling Fast09:30AM to 11:30AM (IST) Advanced Full-Stack .NET Developer Certification Training Sep 15 SAT, SUN Filling Fast09:30AM to 11:30AM (IST) .NET Solution Architect Certification Training Sep 22 SAT, SUN Filling Fast07:00AM to 09:00AM (IST) Software Architecture and Design Training Sep 22 SAT, SUN Filling Fast07:00AM to 09:00AM (IST) Advanced Full-Stack .NET Developer Certification Training Sep 29 SAT, SUN Filling Fast08:30PM to 10:30PM (IST) ASP.NET Core Certification Training Sep 29 SAT, SUN Filling Fast08:30PM to 10:30PM (IST)

Can't find convenient schedule? Let us know

Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.