Swap two numbers in Python

Write a program to swap two numbers in python

Swapping By using the comma operator
Swapping By using Naive Approach

Swapping By using arithmetic operators

Swapping in Python By using the comma operator

Output

Please enter a value for T4tutorials_1: 7
Please enter a value for T4tutorials_2: 9
The Value of T4tutorials_1 after swapping: 9
The Value of T4tutorials_2 after swapping: 7

Swapping in Python By using Naive Approach

Output

Please enter a value for T4tutorials_1: 2
Please enter a value for T4tutorials_2: 4
The Value of P after swapping: 4
The Value of T4tutorials_2 after swapping: 2

Swapping in Python By using arithmetic operators

Output

Please enter a value for T4tutorials_1: 9
Please enter a value for T4tutorials_2: 3
The Value of T4tutorials_1 after swapping: 3
The Value of T4tutorials_2 after swapping: 9

Swapping in Python By using XOR

Output

Please enter a value for T4tutorials_1: 6
Please enter a value for T4tutorials_2: 3
The Value of T4tutorials_1 after swapping:  3
The Value of T4tutorials_2 after swapping:  6

Swapping in Python By using multiplication and division operator


Output

Please enter a value for T4tutorials_1: 55
Please enter a value for T4tutorials_2: 66
The Value of T4tutorials_1 after swapping: 66.0
The Value of T4tutorials_2 after swapping: 55.0

Add a Comment