Calculate Sum Get User Input Patterns Program Find Area of CircleFind Area of Rectangle Perimeter of Rectangle Find Even and Odd Numbers Find Sum of Digit in Python Find Factorial in PythonPrint Fibonacci Series Check Armstrong Number Reverse a Number Check Palindrome Number Swap Two Numbers Matrix MultiplicationFind Perfect NumberAdd Two Matrix in Python Find Sum of Squares Find Sum of Cubes Find URL in String

List Programs

Find Largest in List Find Duplicate in List Check Alphabet Remove Duplicate Elements from List Reverse List Elements Find Element in Python List Interchange first and last element in List Find Even Numbers in List Find Length of a List Find Sum of list Elements Find Elements in List Get the product of List elements Interchange List Elements Find Smallest Elements in List Find Max Value in a List

Swapping is a technique of exchanging values between two variables. We can use this in our programming to exchange values between two variables. In, this article, we will see some solutions to swap values by using, two variables, three variables, or bitwise operator, etc. Let's see some examples

To swap values, we used a temporary variable to hold value and then simply exchanged the values.

```
# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))
print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)
# Swaping the Variables
temp = number1
number1 = number2
number2 = temp
print("After Swapping...")
print("Number1",number1)
print("Number2",number2)
```

**Output:**

Enter First Number 20

Enter Second Number 30

Before Swapping...

Number1 20

Number2 30

After Swapping...

Number1 30

Number2 20

Here, we are using a single assignment operator to swap values between variables. It is a feature of python that allows assigning multiple variables at the same time.

```
# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))
print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)
# Swaping the Variables without third variable
number1, number2 = number2, number1
print("After Swapping...")
print("Number1",number1)
print("Number2",number2)
```

**Output:**

Enter First Number 20

Enter Second Number 30

Before Swapping...

Number1 20

Number2 30

After Swapping...

Number1 30

Number2 20

Here, we are using plus and minus operators to swap values between two variables. It is a nice approach if you want to arithmetic operators.

```
# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))
print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)
# Swaping the Variables without third variable
number1 = number1 + number2
number2 = number1 - number2
number1 = number1 - number2
print("After Swapping...")
print("Number1",number1)
print("Number2",number2)
```

**Output:**

Enter First Number 20

Enter Second Number 30

Before Swapping...

Number1 20

Number2 30

After Swapping...

Number1 30

Number2 20

```
# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))
print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)
# Swaping the Variables without third variable
number1 = number1 ^ number2
number2 = number1 ^ number2
number1 = number1 ^ number2
print("After Swapping...")
print("Number1",number1)
print("Number2",number2)
```

**Output:**

Enter Second Number 30

Before Swapping...

Number1 20

Number2 30

After Swapping...

Number1 30

Number2 20