# How to swap two numbers in Python + Various Examples

In this python tutorial, you will learn about the Python program to swap two numbers, we will see how to swap two numbers in Python and also we will check:

• Python program to swap two numbers
• Python program to swap two numbers in a list
• Python program to swap two numbers using function
• Python program to swap two numbers without using third variable
• Python program to swap two numbers using bitwise XOR
• Python program to swap two numbers using multiplication and division.
• Python program to swap two numbers with using the third variable
• Python program to swap two numbers using class
• Python program to swap two numbers using a temporary variable
• Python program to swap two numbers using tuple
• Python program to swap two numbers entered by a user
• Swapping of three numbers in python
• Swapping of three numbers using a function in python
• Python program to swap two numbers using + and operator.
• Python program to swap four variables without a temporary variable

## Python program to swap two numbers

Here, we can see how to write a program to swap two numbers in python.

• In this example, I have taken two numbers as number1 = 25 and number2 = 30.
• To swap the numbers, I have used number1, number2 = number2, number1.
• To get the output, I have used print(“number1 =”, number1), and print(“number2 =”, number2).

Example:

``````number1 = 25
number2 = 30
number1, number2 = number2, number1
print("number1 =", number1)
print("number2 =", number2)``````

We can see the two numbers are swapped as the output. You can refer to the below screenshot for the output.

The above code, we can use to swap two numbers in Python.

## Python program to swap two numbers in a list

Now, we can see how to write program to swap two numbers in a list in python.

• In this example, I have defined a function as def swapIndex(list, index1, index2).
• The parameters are passed into the function as a list, index1, index2.
• To swap the numbers, I have used list[index1], list[index2] = list[index2], list[index1].
• Then the function is returned as a return list.
• The value to be swapped is mentioned as index1, index2 = 1, 3.
• To get the output, I have used print(swapIndex(List, index1-1, index2-1)).
• In the index1-1, -1 represents the index value in the list starts from 1

Example:

``````def swapIndex(list, index1, index2):
list[index1], list[index2] = list[index2], list[index1]
return list
List = [25, 85, 30, 40]
index1, index2 = 1, 3
print(swapIndex(List, index1-1, index2-1))``````

The numbers from the list are swapped. We can see the output in the below screenshot.

This Python code, we can use to swap two numbers in a list in Python.

You may like:

## Python program to swap two numbers using function

Here, we can see how to write program to swap two numbers using function in Python.

• In this example, I have defined a function called swapnumber as def swapnumbers(a, b).
• Here I have used a variable called temporary variable as temp = a, a = b, and b = temp is assigned to swap the numbers.
• Here, I have used the input() method. To take the inputs from the user.
• To get the output, I have used print(“Before Swapping two Number”,(number1, number2)) and print(“After Swapping two Number”,(a, b)).

Example:

``````def swapnumbers(a, b):
temp = a
a = b
b = temp
print("After Swapping two Number",(a, b))
number1 = int(input(" Please Enter the First number : "))
number2 = int(input(" Please Enter the Second number : "))
print("Before Swapping two Number",(number1, number2))
swapnumbers(number1, number2)``````

We can see the numbers before and after swapping as the output. You can refer to the below screenshot for the output.

This is how to swap two numbers using function in Python.

Check out below Python function examples:

## Python program to swap two numbers without using third variable

Here, we can see how to write program to swap two numbers without using third variable in python

• In this example, I have used input() methods. To take the inputs from the user.
• Instead of using a third variable to swap the numbers.
• The multiplication and division are performed for the numbers
• I have used print(“Swapped numbers of number1 is %d & number2 is %d” %(a,b)) to get the output.

Example:

``````a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
a = a * b
b = a / b
a = a / b
print("Swapped numbers of number1 is %d & number2 is %d" %(a,b))``````

The numbers are swaped as the output. You can refer to the below screenshot for the output.

The above code, we can use to swap two numbers without using third variable in Python.

## Python program to swap two numbers using bitwise XOR

Now, we can see how to write a program to swap two numbers using bitwise XOR in python.

• In this example, I have taken two numbers to swap.
• To swap the number, I have used bitwise XOR like number1 = number1 ^ number2, number2 = number1 ^ number2, number1 = number1 ^ number2.
• I have used print (“After Swapping: number1 = “, number1, ” number2 =”, number2) to get the output.

Example:

``````number1 = 30
number2 = 5
number1 = number1 ^ number2;
number2 = number1 ^ number2;
number1 = number1 ^ number2;
print ("After Swapping: number1 = ", number1, " number2 =", number2)``````

We can see the numbers are swapped as the output. You can refer to the below screenshot for the output.

This is the Python program to swap two numbers using bitwise XOR.

## Python program to swap two numbers using multiplication and division

Now, we can see how to write program to swap two numbers using multiplication and division in python.

• In this example, I have taken two numbers as a = 25 and b = 30.
• We should multiply and divide a, b.
• Again the variable a is assigned as a = a // b.
• I have used print(“After Swapping: a =”,a, ” b =”, b) to get the output.

Example:

``````a = 25
b = 30
a = a * b
b = a // b;
a = a // b;
print("After Swapping: a =",a, " b =", b);``````

The value assigned to a and b are swapped as the output. You can refer to the below screenshot for the output.

The above code, we can use in Python to swap two numbers using multiplication and division.

## Python program to swap two numbers with using the third variable

Here, we can see how to write a program to swap two numbers with using the third variable in python.

• In this example, I have used input() methods. To take the inputs from the user.
• Here, I have used temp as the third variable to swap the numbers.
• I have used print(“Value of number1 is %d” %number1) to get the output.

Example:

``````number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
temp = number1
number1 = number2
number2 = temp
print("Value of number1 is %d" %number1)
print("Value of number2 is %d" %number2)``````

You can refer to the below screenshot for the output.

This code we can use to swap two numbers with using the third variable in Python.

## Python program to swap two numbers using class

Here, we can see how to write program to swap two numbers using class in python.

• In this example, I have created a class using the constructor to initialize the values of the class.
• I have created a method to swap the numbers.
• The self is the keyword used to pass the attributes and methods to the class.
• I have used the input() method to take the inputs from the user.
• The object is created for the class to pass the parameter.
• To display the swapped numbers, I have used obj.display().

Example:

``````class Number:
def __init__(self, x, y):
self.x = x
self.y = y
def swap(self):
temp = self.x
self.x = self.y
self.y = temp
def display(self):
print("After swap a is:", self.x)
print("After swap b is:", self.y)
x = int(input("Enter first number:"))
y = int(input("Enter second number:"))
obj = Number(x, y)
obj.swap()
obj.display()``````

The belowscreenshot shows the output.

This is the Python program to swap two numbers using class.

## Python Program to swap two numbers using a temporary variable

Here, we can see how to write Program to swap two numbers using a temporary variable in python.

• In this example, I have taken two variables and assigned numbers like x = 100, y= 300.
• To swap the numbers, I am using a temporary variable as temp.
• I have used print(“after swapping=”, x, ” y=”, y) to get the output.

Example:

``````x = 100
y= 300
print("before swapping=", x, " y=", y)
temp = x
x = y
y = temp
print("after swapping=", x, " y=", y)``````

You can refer to the below screenshot for the output.

This is the Python code to swap two numbers using a temporary variable in Python.

## Python program to swap two numbers using tuple

Now, we can see how to write program to swap two numbers using tuple in python.

• In this example, I have used the input() method. To take the inputs from the user.
• I have used (x,b)=(b,x) to swap the numbers.
• I have print (“x=”,x, “b=”,b) to get the output.

Example:

``````x=int(input("Enter the first number :"))
b=int(input("Enter the second number :"))
print ("x=",x, "b=",b)
(x,b)=(b,x)
print("x=",x, "b=",b)``````

The number are swapped as the output. You can refer to the below screenshot for the output.

This is the Python code to swap two numbers using tuple in Python.

## Python program to swap two numbers entered by the user

Here, we can see how to write program to swap two numbers entered by the user in python

• In this example, I have used the input() method. To take the inputs from the users.
• Here, I have used the temp variable to swap the numbers.
• I have used print((a, b)) to get the output.

Example:

``````a = float(input(" Enter the First number "))
b = float(input(" Please Enter the Second number b: "))
print((a, b))
temp = a
a = b
b = temp
print((a, b))``````

We can see that numbers are entered by the user and then swapped as the output. You can refer to the below screenshot for the output.

This is the Python program to swap two numbers entered by the user.

## Swapping of three numbers in Python

Here, we can see how to write program of swapping of three numbers in python.

• In this example, I have used the input() method to take the inputs from the user.
• Here, I have assigned the variable value to a. The a’s value to b, b’s value to c, and c’s value to a variable.
• To get the output, I have used print(‘The value of a after swapping is {}’.format(a)).

Example:

``````a = input("Enter a first number")
b = input("Enter a second number")
c = input("Enter a third number")
print('The value of a is {}'.format(a))
print('The value of b is {}'.format(b))
print('The value of c is {}'.format(c))
variable = a
a = b
b = c
c = variable
print('The value of a after swapping is {}'.format(a))
print('The value of b after swapping is {}'.format(b))
print('The value of c after swapping is {}'.format(c))``````

You can refer to the below screenshot for the output.

This code, we can use to swap of three numbers in Python.

## Swap of three numbers using a function in python

Here, we can see how to swap of three numbers using a function in python.

• In this example, I have defined a function called def swapThree(x, y, z).
• The sum of value is stored in x, next y stores the value of x, after this x stores the value of y.
• After this x stores the value for z.
• The if __name__ == ‘__main__’ is used to execute the code when the file is not executed.
• The values to be swapped are assigned to the variables.
• To get the output print(“Before swapping x =”,x,”, y =”,y,”, z =”,z) and print(“After swapping x =”,x,”, y =”,y,”, z =”,z) is used.

Example:

``````def swapThree(x, y, z):
x = x + y + z
y = x - (y+z)
z = x - (y+z)
x = x - (y+z)
print("After swapping x =",x,", y =",y,", z =",z)
if __name__ == '__main__':
x = 2
y = 4
z = 6
print("Before swapping x =",x,", y =",y,", z =",z)
swapThree(x, y, z)``````

You can refer to the below screenshot for the output.

This is the Python code to swap of three numbers using a function in python.

## Python program to swap two numbers using + and – operator

Here, we can see how to write program to swap two numbers using + and – operator in python

• In this example, I have used float data type to enter the inputs in decimal format.
• The two numbers are added and then subtracted. Again it is assigned to the variable x.
• I have used print(“Swapped values of x is %.1f & y is %.1f” %(x,y)) to get output.
• The .1f is the placeholder for floating numbers.

Example:

``````x = float(input("Enter the first number: "))
y = float(input("Enter the second number: "))
x = x + y
y = x - y
x = x - y
print("Swapped values of x is %.1f & y is %.1f" %(x,y))``````

You can refer to the below screenshot for the output.

This is the Python code to swap two numbers using + and – operator in Python.

## Python program to swap four variables without a temporary variable

Now, we can see how to write a program to swap four variables without temporary variable in python

• In this example, I have defined a function as def swap(a, b, c, d).
• The a = a + b, b = a – b, a = a – b is used to swap variable a and b.
• The a = a + b, b = a – b, a = a – b is used to swap variable b and c.
• The c = c + d, d = c – d, c = c – d is used to swap variable c and d.
• Then the values are initialized to the variable. The function is called as swap(a, b, c, d).

Example:

``````def swap(a, b, c, d):
a = a + b
b = a - b
a = a - b

b = b + c
c = b - c
b = b - c

c = c + d
d = c - d
c = c - d

print("values after swapping are : ")
print("a = ", a)
print("b = ", b)
print("c = ", c)
print("d = ", d)
a = 1
b = 2
c = 3
d = 4
print("values before swapping are : ")
print("a = ", a)
print("b = ", b)
print("c = ", c)
print("d = ", d)
print("")
swap(a, b, c, d)``````

All the four variables are swapped as the output. You can refer to the below screenshot for the output.

This is the code to swap four variables without a temporary variable in Python.

## Python program to swap three numbers without a temporary variable

Here, we can see how to write a program to swap three numbers without a temporary variable in python.

• In this example, I have assigned the values to the variables.
• Then to swap the numbers x = x + y + z, y = x – (y+z), z = x – (y+z), x = x – (y+z) addition and substraction operation is performed.
• I have used print(“Before swapping a = “,x,”, b = “,y,”, c = “,z) and print(“After swapping a = “,x,”, y = “,y,”, z = “,z) to get the output.

Example:

``````x = 14
y = 12
z = 97
print("Before swapping a = ",x,", b = ",y,", c = ",z)
x = x + y + z
y = x - (y+z)
z = x - (y+z)
x = x - (y+z)
print("After swapping a = ",x,", y = ",y,", z = ",z) ``````

We can see the swapped numbers as the output. YOu can refer to the below screenshot for the output.

This is the Python code to swap three numbers without a temporary variable in Python.

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python programs to swap two numbers. Also, we covered these below topics:

• Python program to swap two numbers
• Python program to swap two numbers in a list
• Python program to swap two numbers using function
• Python program to swap two numbers without using third variable
• Python program to swap two numbers using bitwise XOR
• Python program to swap two numbers using multiplication and division.
• Python program to swap two numbers with using the third variable
• Python program to swap two numbers using class
• Python Program to Swap two numbers using a temporary variable
• Python program to swap two numbers using tuple
• Python program to swap two numbers entered by a user
• Swapping of three numbers in python
• Swapping of three numbers using a function in python
• Python program to swap two numbers using + and operator.
• Python program to swap four variables without a temporary variable