Python Program to Swap Two Variables

Posted in

Python Program to Swap Two Variables
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    In this article, we have provided a python source code that is capable of swapping the value of two variables. Swapping the value of two variables is a very common and easy coding problem and in every programming language, you can easily make this program.

    Prerequisite for Python Program to Swap Two Variables

    • Python Input, Output
    • Python Data types
    • Python Operators

    Swap two variables using a temporary variable

    Steps

    • First, we ask the user to enter the values of both variables a and b.
    • Then we store the value of an in temporary value and store the value of b in a.
    • At last, we put the value of the temporary variable in b.

    Code

    a = input("Enter the value of a: ")
    b = input("Enter the value of b: ")
    
    #variable swapping logic
    temp = a
    a=b
    b=temp
    
    print("After swapping the value of a has become: ",a)
    print("After swapping the value of b has become: ",b)

    Input

    Enter the value of a: 200
    Enter the value of b: 900

    Output

    Enter the value of a: 200
    Enter the value of b: 900
    After swapping the value of a has become:  900
    After swapping the value of b has become:  200

    Swapping the values of two variables without Using a Temporary Variable

    In the above example, we have used a temporary variable to swap the values of the variable but python also provides us a technique call tuple unpacking by which we can swap the values of two variables in a single line.

    Code

    a = input("Enter the value of a: ")
    b = input("Enter the value of b: ")
    
    #variable swapping using tuple unpacking
    a,b = b,a
    
    print("After swapping the value of a has become: ",a)
    print("After swapping the value of b has become: ",b)

    Output

    Enter the value of a: 132
    Enter the value of b: 1900
    After swapping the value of a has become:  1900
    After swapping the value of b has become:  132

    Swap the value of two integer variable without using Addition and Subtraction

    Using the logic of Addition and Subtraction we can swap the values of two integer values:

    Logic

    a = a+b b= a-b a= a-b

    Code

    a = int(input("Enter the value of a: "))
    b = int(input("Enter the value of b: "))
    
    # integer variable swapping using tuple Addition and Subtraction
    a = a+b
    b = a-b
    a = a-b 
    
    print("After swapping the value of a has become: ",a)
    print("After swapping the value of b has become: ",b)

    Output

    Enter the value of a: 100
    Enter the value of b: 120
    After swapping the value of a has become:  120
    After swapping the value of b has become:  100

    Apart from addition and subtraction, we can use multiplication and division operator to swap two integer type variables.

    Multiplication and Division

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

    XOR swap

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

    People are also reading:

    Leave a Comment on this Post

    0 Comments