Python Program to Swap Two Variables: With & Without Temp Variable

Python Program to Swap Two Variables: With & Without Temp Variable

How to Swap Two Numbers in Python: Simple and Efficient Methods

Swapping two numbers is a fundamental programming task that can be accomplished in several ways. Whether you’re a beginner or an experienced Python developer, understanding different methods of swapping can improve your coding skills. In this article, we will explore various ways to swap two numbers in Python — with and without a temporary variable.

Table of Contents

  • Introduction
  • Input and Output Format
  • Algorithm to Swap Two Numbers
  • Method 1: Swap Two Numbers Using a Third Variable
  • Method 2: Swap Two Numbers Using Arithmetic Operators
  • Method 3: Swap Two Numbers Using Multiplication and Division
  • Method 4: Swap Two Numbers Using Bitwise XOR
  • Method 5: Swap Two Numbers Using Functions
  • Conclusion

Introduction: Understanding the Concept of Swapping in Python

In many scenarios, you may need to swap the values of two variables. For example, you might want to switch the values of a and b such that if a = 5 and b = 7, after swapping, a = 7 and b = 5. Python provides several ways to achieve this, and each method has its own benefits.

Input and Output Format

Before we dive into the methods, let’s define how the input and output will work:

Input Format:

  • The first input corresponds to an integer x.
  • The second input corresponds to another integer y.

Output Format:

  • The program will print the swapped values of x and y.

Algorithm to Swap Two Numbers in Python

Here’s a simple breakdown of the steps to swap two numbers:
  1. Get input from the user: Use the input() function to accept two integer values, x and y, from the user.
  2. Perform the swap: Depending on the chosen method, swap the values of x and y.
  3. Print the result: Display the swapped values.

Method 1: Swap Two Numbers Using a Third Variable

This is the classic approach where we use an extra variable to temporarily hold the value of one of the numbers while swapping.
python
# Python program to swap two variables using a third variable# Taking input from the user x = int(input(“Enter the first number: “)) y = int(input(“Enter the second number: “))# Create a temporary variable and swap the values temp = x x = y y = temp# Output the swapped values print(“Swapped values: x =”, x, “and y =”, y)

Input Example:

mathematica
Enter the first number: 23 Enter the second number: 25

Output:

java
Swapped values: x = 25 and y = 23
While effective, using a third variable is not the most memory-efficient method.

Method 2: Swap Two Numbers Using Arithmetic Operators

This method eliminates the need for a third variable by utilizing arithmetic operators like + and -. It’s a clever way to swap numbers without additional memory usage.
python
# Python program to swap two variables without using a third variable# Taking input from the user a = float(input(“Enter the first number: “)) b = float(input(“Enter the second number: “))# Swapping using arithmetic operators a = a + b b = a – b a = a – b# Output the swapped values print(“Swapped values: x =”, a, “and y =”, b)

Input Example:

mathematica
Enter the first number: 5.8 Enter the second number: 2.6

Output:

java
Swapped values: x = 2.6 and y = 5.8
This method works well for integer values, but be cautious when working with floating-point numbers due to rounding errors.

Method 3: Swap Two Numbers Using Multiplication and Division

This is another arithmetic method, but instead of + and -, it uses the multiplication (*) and division (/) operators. However, it has the potential to result in overflow if the numbers are very large.
python
# Python program to swap two variables without using a third variable# Taking input from the user a = int(input(“Enter the first number: “)) b = int(input(“Enter the second number: “))# Swapping using multiplication and division a = a * b b = a / b a = a / b# Output the swapped values print(“Swapped values: x =”, a, “and y =”, b)

Input Example:

mathematica
Enter the first number: 54 Enter the second number: 99

Output:

java
Swapped values: x = 99 and y = 54
Although this method works well, it is not recommended for decimal numbers due to potential precision issues.

Method 4: Swap Two Numbers Using Bitwise XOR

The bitwise XOR (^) operator can also be used to swap two variables without a third variable. This method is often considered one of the most efficient because it doesn’t involve arithmetic operations or extra memory.
python
# Python program to swap two variables using bitwise XOR# Taking input from the user a = int(input(“Enter the first number: “)) b = int(input(“Enter the second number: “))# Swapping using bitwise XOR a = a ^ b b = a ^ b a = a ^ b# Output the swapped values print(“Swapped values: x =”, a, “and y =”, b)

Input Example:

mathematica
Enter the first number: 548 Enter the second number: 847

Output:

java
Swapped values: x = 847 and y = 548
This is a very efficient method for integer values and avoids overflow or precision errors.

Method 5: Swap Two Numbers Using Functions

If you prefer a more structured approach, you can define a function to swap the numbers. This method encapsulates the logic in a function, making the code cleaner and reusable.
python
# Python program to swap two variables using functionsdef swap(a, b): # Swap logic a, b = b, a return a, b# Taking input from the user a = float(input(“Enter the first number: “)) b = float(input(“Enter the second number: “))# Call the swap function a, b = swap(a, b)# Output the swapped values print(“Swapped values: x =”, a, “and y =”, b)

Input Example:

mathematica
Enter the first number: 54.2648 Enter the second number: 25.1239

Output:

java
Swapped values: x = 25.1239 and y = 54.2648
This approach is modular, making it easy to integrate into larger projects.

Conclusion

Swapping two numbers in Python can be done in several ways. The method you choose depends on your specific needs, such as memory efficiency or ease of understanding. While Method 2 (using arithmetic operators) is simple and efficient for small numbers, Method 4 (using bitwise XOR) is considered the most efficient for integers.We hope this guide has helped you understand how to swap two variables in Python using different methods. Click Here to Know more our program!