# Python Program to Swap Two Variables

Swapping:

Swapping two variables in computer programming means that the variables values are swapped.

Given two variables x , y the task is to swap the variable values.

Examples:

Example1:(Integer Values Swapping)

Input:

p=1372 q=4129

Output:

printing the values of integer variables p and q before swapping
p = 1372
q = 4129
printing the values of integer variables p and q after swapping
p = 4129
q = 1372

Example2:(Boolean Values Swapping)

Input:

p=True q=False

Output:

printing the values of boolean variables p and q before swapping
p = True
q = False
printing the values of boolean variables p and q after swapping
p = False
q = True

Example3:(Decimal Values Swapping)

Input:

p = 2738.321  q = 83472.421

Output:

printing the values of decimal variables p and q before swapping
p = 2738.321
q = 83472.421
printing the values of decimal variables p and q after swapping
p = 83472.421
q = 2738.321

Example4:(String Values Swapping)

Input:

p="Vicky" q="Tara"

Output:

printing the values of string variables p and q before swapping
p = vicky
q = Tara
printing the values of string variables p and q after swapping
p = Tara
q = vicky

## Swapping two Variables in Python

There are several ways to swap two variables in Python some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

### Method #1: Using temporary Variable

A temp variable is the easiest way to change the values of two variables. The temp variables will save the value of the fist variable (temp = a), allow the two variables to swap (a = b), and assign the them to the second variable. The temp variables will then be saved.

Below is the implementation:

# given variables of different types
# given two integer variables
p = 1372
q = 4129
# printing the values of p and q before swapping
print("printing the values of integer variables p and q before swapping")
print("p =", p)
print("q =", q)
# using temp variable to swap the two integers
# swapping two variables
tempo = p
p = q
q = tempo
# printing the values of p and q after swapping
print("printing the values of integer variables p and q after swapping")
print("p =", p)
print("q =", q)
# given two decimal variables
p = 2738.321
q = 83472.421
# printing the values of p and q before swapping
print("printing the values of decimal variables p and q before swapping")
print("p =", p)
print("q =", q)
# using temp variable to swap the two decimal variables
# swapping two variables
tempo = p
p = q
q = tempo
# printing the values of p and q after swapping
print("printing the values of decimal variables p and q after swapping")
print("p =", p)
print("q =", q)
# given two boolean variables
p = True
q = False
# printing the values of p and q before swapping
print("printing the values of boolean variables p and q before swapping")
print("p =", p)
print("q =", q)
# using temp variable to swap the two boolean values
# swapping two variables
tempo = p
p = q
q = tempo
# printing the values of p and q after swapping
print("printing the values of boolean variables p and q after swapping")
print("p =", p)
print("q =", q)
# given two string variables
p = "vicky"
q = "Tara"
# printing the values of p and q before swapping
print("printing the values of string variables p and q before swapping")
print("p =", p)
print("q =", q)
# using temp variable to swap the two string
# swapping two variables
tempo = p
p = q
q = tempo
# printing the values of p and q after swapping
print("printing the values of string variables p and q after swapping")
print("p =", p)
print("q =", q)


Output:

printing the values of integer variables p and q before swapping
p = 1372
q = 4129
printing the values of integer variables p and q after swapping
p = 4129
q = 1372
printing the values of decimal variables p and q before swapping
p = 2738.321
q = 83472.421
printing the values of decimal variables p and q after swapping
p = 83472.421
q = 2738.321
printing the values of boolean variables p and q before swapping
p = True
q = False
printing the values of boolean variables p and q after swapping
p = False
q = True
printing the values of string variables p and q before swapping
p = vicky
q = Tara
printing the values of string variables p and q after swapping
p = Tara
q = vicky

### Method #2:Using comma operator in Python without temporary variable(Tuple Swap)

The tuple packaging and sequence unpackaging can also be used to change the values of two variables without a temporary variable. There are a number of methods in which tuples can be created, including by dividing tuples using commas. In addition, Python evaluates the right half of a task on its left side. Thus the variables are packed up and unpacked with the same amount of commas separating the target variables on the left hand side by selecting the comma on the right hand hand side of the sentence.

It may be used for more than two variables, provided that both sides of the state have the same amount of variables

Below is the implementation:

# given variables of different types
# given two integer variables
p = 1372
q = 4129
# printing the values of p and q before swapping
print("printing the values of integer variables p and q before swapping")
print("p =", p)
print("q =", q)
# swapping two variables
p, q = q, p
# printing the values of p and q after swapping
print("printing the values of integer variables p and q after swapping")
print("p =", p)
print("q =", q)
# given two decimal variables
p = 2738.321
q = 83472.421
# printing the values of p and q before swapping
print("printing the values of decimal variables p and q before swapping")
print("p =", p)
print("q =", q)
# swapping two variables
p, q = q, p
# printing the values of p and q after swapping
print("printing the values of decimal variables p and q after swapping")
print("p =", p)
print("q =", q)
# given two boolean variables
p = True
q = False
# printing the values of p and q before swapping
print("printing the values of boolean variables p and q before swapping")
print("p =", p)
print("q =", q)
# swapping two variables
p, q = q, p
# printing the values of p and q after swapping
print("printing the values of boolean variables p and q after swapping")
print("p =", p)
print("q =", q)
# given two string variables
p = "vicky"
q = "Tara"
# printing the values of p and q before swapping
print("printing the values of string variables p and q before swapping")
print("p =", p)
print("q =", q)
# swapping two variables
p, q = q, p
# printing the values of p and q after swapping
print("printing the values of string variables p and q after swapping")
print("p =", p)
print("q =", q)


Output:

printing the values of integer variables p and q before swapping
p = 1372
q = 4129
printing the values of integer variables p and q after swapping
p = 4129
q = 1372
printing the values of decimal variables p and q before swapping
p = 2738.321
q = 83472.421
printing the values of decimal variables p and q after swapping
p = 83472.421
q = 2738.321
printing the values of boolean variables p and q before swapping
p = True
q = False
printing the values of boolean variables p and q after swapping
p = False
q = True
printing the values of string variables p and q before swapping
p = vicky
q = Tara
printing the values of string variables p and q after swapping
p = Tara
q = vicky

Related Programs: