• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to secondary sidebar

CodingStatus

- Level Up Your Status with Coding Expertise!

  • Tutorials
    • React Js Tutorials
    • JavaScript Tutorials
    • PHP Tutorials
    • HTML Tutorials
    • SQL Tutorials
    • Laravel Tutorials
  • Snippets
    • React Js Code Snippets
    • Ajax Code Snippets
    • Node.js Code Snippets
    • JavaScript Code Snippets
    • jQuery Code Snippets
    • SQL Code Snippets
  • Programs
    • PHP Program
    • Python Programs
    • Java Programs
  • How To
    • React Js How To
    • Node Js How To
    • Ajax How To
    • PHP How To
    • jQuery How To
    • JavaScript How to
  • Scripts
    • PHP Script
    • Js Script
    • React Js Script
    • JQuery Script
  • Projects
    • PHP Project
  • Interview Questions
  • Guide
  • API Integration
  • Installation

Swapping of Two Numbers in python

March 26, 2023 By Md Nurullah

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.

Table of Contents

  • Python Program to Swap Two Numbers with a Third Variable
  • Python Program to Swap Two Numbers without a Third Variable.
  • Python Program to Swap Two Numbers with Input
  • Program to Swap Two Numbers Using Functions

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()

 

Filed Under: Python Program

Hello Developer, Welcome to my Blog. I'm Md Nurullah, a software engineer with 5+ years in full-stack web development. As the founder of CodingStatus.com, I'm passionate about sharing coding knowledge and collaborating for a brighter future. Let's connect and code together!

Primary Sidebar

Secondary Sidebar

Python Program,
  • Check Armstrong Number in Python
  • Check Leap Year in Python
  • Print prime numbers from 1 to n in python
  • Swapping of Two Numbers in python
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2023 CodingStatus - All Rights Reserved