Python Program to Swap Two Numbers

In this python tutorial, you will learn how to Swap Two Number using arithmetic operators and print function of the python programming language.

How to Swap Two Numbers in Python?

Let’s take a look at the first source code , here the integer values are assigned in the code and the arithmetic operator = carries out the function.

RUN CODE SNIPPET
# Python code to swap two numbers

x = 45
y = 70

print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)

# code to swap 'x' and 'y'
x, y = y, x

print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)

OUTPUT:

Before swapping: 
Value of x : 45 and y : 70
After swapping: 
Value of x : 70 and y : 45
  1. At the start, we declare the variables x and y with the integer values 45 and 70 .
  2. We display the Statements "Before Swapping: "  followed by the statement ("Value of x : ", x, " and y : ", y) which holds the declared variables after the comma. It is displayed using the print function.
  3. The swap code here uses the equals operator =, which has a built function that swaps the input integers and displays the output, the code goes as x, y = y, x
  4. We display the Statements "After Swapping: " after the swap code, followed by the statement ("Value of x : ", x, " and y : ", y) which holds the declared variables after the comma. It is displayed using the print function.

Let’s take a look at the second source code, here the integer values are assigned in the code and the arithmetic operator + and – carries out the function.

RUN CODE SNIPPET
# Python code to swap two numbers using + and - operators


x = 12.7
y = 4.3

print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)

# Swap code
x = x + y # x = 17.0, y = 4.3
y = x - y # x = 17.0, y = 12.7
x = x - y # x = 4.3, y = 12.7

print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)

OUTPUT:

Before swapping: 
Value of x : 12.7 and y : 4.3
After swapping: 
Value of x : 4.3 and y : 12.7
  1. At the start, we declare the variables x and y with the integer values 12.7 and 4.3 .
  2. We display the Statements "Before Swapping: "  followed by the statement ("Value of x : ", x, " and y : ", y) which holds the declared variables after the comma. It is displayed using the print function.
  3. The swap code here uses the addition and subtraction operators (+ and -), which has a built function that swaps the input integers and displays the output, the code goes as x = x + y, y = x - y and x = x - y respectively one below the other.
  4. We display the Statements "After Swapping: " after the swap code, followed by the statement ("Value of x : ", x, " and y : ", y) which holds the declared variables after the comma. It is displayed using the print function.

Let’s take a look at the third source code, here the integer values are assigned in the code and the arithmetic operator * and / carries out the function.

RUN CODE SNIPPET
# Python code to swap two numbers using / and * operators

x = 12.7
y = 4.3

print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)

# Swap code
x = x * y # x = 54.61, y = 4.3
y = x / y # x = 54.61, y = 12.7
x = x / y # x = 4.3, y = 12.7

print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)

OUTPUT:

Before swapping: 
Value of x : 12.7 and y : 4.3
After swapping: 
Value of x : 4.3 and y : 12.7
  1. At the start, we declare the variables x and y with the integer values 12.7 and 4.3 .
  2. We display the Statements "Before Swapping: "  followed by the statement ("Value of x : ", x, " and y : ", y) which holds the declared variables after the comma. It is displayed using the print function.
  3. The swap code here uses the multiplication and division operators (* and /), which has a built function that swaps the input integers and displays the output, the code goes as x = x * y, y = x / y and x = x / y respectively one below the other.
  4. We display the Statements "After Swapping: " after the swap code, followed by the statement ("Value of x : ", x, " and y : ", y) which holds the declared variables after the comma. It is displayed using the print function.

NOTE:

  • The equals operator is the simple built – in method which is easy to use.
  • The addition and subtraction method of swapping numbers is used for variables which are assigned numerical values and not strings values or any other.
  • The multiplication and division method of swapping number is used for variables which are assigned numerical values other than zero.
  • The print statement/string to be displayed in enclosed in double quotes.
  •  The integers are zero, positive or negative whole numbers without a fractional part.

Leave A Reply

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

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...