Swapping of Two Numbers in python

In this tutorial, You will learn the swapping of two numbers in python with or without a third variable with a complete explanation that will be very helpful to grow your programming skill.

First of all, Let’s know the concept of swapping numbers –

In the case of swapping numbers, We have to exchange the value of two variables. This means that a number N1 is stored in variable V1 and another number N2 is stored in another variable V2.

# Befor swapping
V1 = N1
V2 = N2

If we store a Number  N1 in the variable V2 and another number N2 in the Variable V2. Then This process will be the swapping numbers.

#after swapping
V1 = N2 
V2 = N1

So, We will write a python program to swap two numbers using the swapping concept.

Python Program to Swap Two Numbers with a Third Variable

Write a python program to swap two numbers with a third variable.

Steps to write a program

  • First of all Print a message ‘Number before swapping: 25, 10
  • Store values 25 & 10 in the variables num1 & num2 respectively
  • Also, assign num1 to the temporary variable temp
  • Now, assign to the num2 in the num1
  • Also, assign  temp to the num2
  • Finally, print the swap values of num1 & num2

Source code

print("Numbers before swapping: 25, 10")
num1 = 25
num2 = 10

temp = num1
num1 = num2
num2 = temp

print('Numbers after swapping:', format(num1) + ',' + format(num2))

Python Program to Swap Two Numbers without a Third Variable.

Write a python program to swap two numbers without a third variable.

Steps to Write a Program –

  • First of all Print a message ‘Number before swapping: 25, 10
  • Store values 25 & 10 in the variables num1 & num2 respectively.
  • Add both variables num1 & num2 and then assign to the num1
  • apply if condition num1>num2. if num1 is greater than num2 then write the following steps within the if a block of statement.
    • subtract num2 from num1 and assign to the num2
    • Also, subtract num1 from num2 and assign it to num1.
  • apply if condition num2>num1. if num2 is greater than num1 then write the following steps within the if a block of statement.
    • subtract num2 from num1 and assign to the num2
    • Also, subtract num2 from num1 and assign it to num1.
  • Print both swapped numbers using print().

Source Code –

# swap two numbers with third variable
print("Numbers befor swapping: 25, 30")
num1 = 25
num2 = 30

num1 = num1 + num2

if num1 > num2:
    num2 = num1 - num2
    num1 = num1 - num2
if num2 > num1:
    num2 = num2 - num1
    num1 = num2 - num1


print('Numbers after swapping:', format(num1) + ',' + format(num2))



Python Program to Swap Two Numbers with Input

Write a python program to swap two numbers with input.

Steps to write a program

  • First of all, declare the input() method to take the first number and assign it to the num1
  • Also, declare another input method to take the second number and assign to the num2
  • Print the input numbers to show the message before swapping
  • Also, assign num1 to the temporary variable temp
  • Now, assign to the num2 in the num1
  • Also, assign  temp to the num2
  • At last, print the message after swapping

Source Code –

# swap two numbers using input

num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')

print('Numbers before swapping:', format(num1) + ',' + format(num2))

temp = num1
num1 = num2
num2 = temp

print('Numbers after swapping:', format(num1) + ',' + format(num2))




Program to Swap Two Numbers Using Functions

Write a program to swap two numbers using functions

Steps to write a program

  • Print the input numbers 25 & 10 to show the message before swapping
  • declare a function swapTwoNumbers() and implement the following steps within it
    • assign num1 to the temporary variable temp
    • Now, assign to the num2 in the num1
    • Also, assign  temp to the num2
    • At last, print the message after swapping
  • Call the created function swapTwoNumbers()

Source Code –

# swap two numbers with a third variable
print("Numbers before swapping: 25, 30")

def swapTwoNumbers():
    num1 = 25
    num2 = 10

    temp = num1
    num1 = num2
    num2 = temp

    print('Numbers after swapping:', format(num1) + ',' + format(num2))

swapTwoNumbers()