adplus-dvertising
Python Program To Swap Two Variables

Python Program To Swap Two Variables

Python Program To Swap Two Variables – Before Discuss Swap Python , Python Swap, Swap in Python This Program We Have To Read Basic PYTHON PROGRAMMING TUTORIAL.

Python Program To Swap Two Variables

In This Lesson or Article, We are Teaching You How Many Ways Are There in Python Programming For Swap. So, From This, We Should Learn How to Swap With Variables and Without Variables in Python Language. If Others Method Available Except These Two methods, Then We Also should learn That Methods. So Read Properly And Understand By Own Knowledge.

Suppose, We have Two Variables Like “S” and “D” ,  Then We have to Write Python Program For Swapping Their values. But If  You Used Extra Variables For Swapping, Then Your Memory Blocked Will be Increase Because of Three Varibles Used and Memory Blocked for these Three Variables. But at That Moment If You Use no Extra Varibles Then Extra Memory is Not Blocked, That Means Your Memory Will be used For two Veriables only.

To Understand This Program You Should have the Knowledge of This Following PYTHON PROGRAM TOPIC :

If You Want to Learn All This Things Then Visit Our PYTHON PROGRAMMING TUTORIAL and Search This Topic.

  • Input/Output and Import Using Python
  • Data Type in Python
  • Python Operators

VISIT OUR OFFICIAL SITE

Python Program To Swap Two Variables Methods

Using Naive approach / Using a temporary variable :

In this method, We have using naive approach or using a temporary variable in python That will store the value of the “S” variable in a temporary variable. Then “S” will assign to the variable “S” with the value of the “D” variable. Then, “D” will assign to the value of the temporary variable to the “D” variable, that will finally swapping the values of both the variable.

Example:

S = int( input(“Please enter 1st value of S: “))
D = int( input(“Please enter 2nd value of D: “))
#To swap the value of two variables
#we will use third variable which is a temporary variable
temporary = S
S = D
D = temporary

print (“The Value of S after swapping: “, S)
print (“The Value of D after swapping: “, D)

OUTPUT:

Please enter value for S: 11
Please enter value for D: 12
The Value of S after swapping: 12
The Value of D after swapping: 11

Python program to swap two variables without using third

By using comma operator :

In this Method, We have using By using comma operator in Python That Will store value directly without use any third variables. Just Write The Program and Declare Variables By Opposite Directions then It will automatically Swapping.

Example:

S = int( input(“Please enter 1st value of S: “))
D = int( input(“Please enter 2nd value of D: “))

#To swap the value of two variables
#we will use comma operator for without use third variable

S ,D= D,S

#Avobe Statement Means S=D and D=S

print (“The Value of S after swapping: “, S)
print (“The Value of D after swapping: “, D)

OUTPUT:

Please enter value for S: 11
Please enter value for D: 12
The Value of S after swapping: 12
The Value of D after swapping: 11

Python program to swap two variables without using a temporary variable

By using XOR method :

Bitwise XOR : – A bitwise XOR is a binary operation which is taken two bits with equal length and performing the logical exclusive OR operation.

In this Method, We have to use Bitwise XOR methods. The XOR of two variables, S and D, should be returning the number that has all the bits as 1 whenever the bits of the S and D variables differents. Like, XOR of 4 and 6 is 1010 because we are know that binary of 4 is 0100 and binary of 6 is 0110.

Example:

S = int( input(“Please enter 1st value of S: “))
D = int( input(“Please enter 2nd value of D: “))

#To swap the value of two variables
#we will use Bitwise XOR Methods for without use third variable

S =S^D

D =S^D

S =S^D

print (“The Value of S after swapping: “, S)
print (“The Value of D after swapping: “, D)

OUTPUT:

Please enter value for S: 11
Please enter value for D: 12
The Value of S after swapping: 12
The Value of D after swapping: 11

write a program in python to swap two variables without using temporary variable

By Using Arithmetic Operation Method :

If both the variables are numbers, we can use arithmetic operations. But if you think about it and want a Example, then it is look like very easy to show it out. Here are a few examples for you :

Using Addition and Substraction:

Example:

S = int( input(“Please enter 1st value of S: “))
D = int( input(“Please enter 2nd value of D: “))

#To swap the value of two variables
#we will use addition first and substractions last Methods for without use third variable

S =S+D

D =S-D

S =S-D

print (“The Value of S after swapping: “, S)
print (“The Value of D after swapping: “, D)

OUTPUT:

Please enter value for S: 11
Please enter value for D: 12
The Value of S after swapping: 12
The Value of D after swapping: 11

Using Multiplication and Division Method :

Example:

S = int( input(“Please enter 1st value of S: “))
D = int( input(“Please enter 2nd value of D: “))

#To swap the value of two variables
#we will use Multiplication and Division Methods for without use third variable

S =S*D

D =S/D

S =S/D

print (“The Value of S after swapping: “, S)
print (“The Value of D after swapping: “, D)

OUTPUT:

Please enter value for S: 11
Please enter value for D: 12
The Value of S after swapping: 12
The Value of D after swapping: 11

Yhaa You have done Swap Python , Python Swap, Swap in Python but next? if YOU Want to your Others Programming In Python and Python Programming Tutorial Then Follow US HERE and Join Telegram.

Related Posts

One thought on “Python Program To Swap Two Variables

  1. Awesome blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog stand out. Please let me know where you got your theme. Thanks

Leave a Reply

Your email address will not be published.