Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Program to Swap Two Numbers

1 min read

Problem Definition

Create a Python program to swap two numbers taking runtime inputs. Swapping means interchanging, for instance, say we have two variables holding the following values a = 10 & b = 20 after swapping they will interchange the values so now a = 20 & b = 10.

Solution

Generally, a third variable is used for swapping where first we store the value of the first variable to a temporary variable say temp, then store the value of the second variable to the first variable and finally, store the value of the temporary variable to the second variable.

Fortunately, Python comes with a simple built-in method to swap two numbers without using any third variable.

 a,b = b,a

This one-liner can swap values of two variable without using any additional variable, let's see this in action in the program below.

Algorithm

  1. Take input from the user and store them in variables
  2. Print the values before swapping
  3. Print the values after swapping
  4. End

Program

num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')
print("Value of num1 before swapping: ", num1)
print("Value of num2 before swapping: ", num2)

num1, num2 = num2, num1

print("Value of num1 after swapping: ", num1)
print("Value of num2 after swapping: ", num2)

Runtime Test Cases

Enter First Number: 2
Enter Second Number: 3

Value of num1 before swapping:  2
Value of num2 before swapping:  3
Value of num1 after swapping:  3
Value of num2 after swapping:  2

The values inside the variables are interchanged; hence we swapped two numbers without using any third variable in Python.


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read