BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Swap Two Numbers

By Chaitanya Singh | Filed Under: Python Examples

In this tutorial we will see two Python programs to swap two numbers. In the first program we are swapping the numbers using temporary variable and in the second program we are swapping without using temporary variable.

Program 1: Swapping two numbers using temporary variable

In this program we are using a temporary variable temp to swap the two numbers. We are storing the value of num1 in temp so that when the value of num1 is overwritten by num2 we have the backup of the num1 value, which we later assign to the num2.

# Program published on https://beginnersbook.com

# Python program to swap two variables

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

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

# swapping two numbers using temporary variable
temp = num1
num1 = num2
num2 = temp

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

Output:

Enter First Number: 101
Enter Second Number: 99
Value of num1 before swapping:  101
Value of num2 before swapping:  99
Value of num1 after swapping:  99
Value of num2 after swapping:  101

Python program swap two numbers

Program 2: Swapping two numbers without using temporary variable

# Program published on https://beginnersbook.com

# Python program to swap two variables

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

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

# swapping two numbers without using temporary variable
num1, num2 = num2, num1

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

Output:

Enter First Number: 101
Enter Second Number: 999
Value of num1 before swapping:  101
Value of num2 before swapping:  999
Value of num1 after swapping:  999
Value of num2 after swapping:  101

Related Python Examples:

1. Python program to reverse a string
2. Python program to check leap year
3. Python program to find the length of a string
4. Python program to find factorial
5. Python program to display Calendar

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap