Python – Swap Two Numbers
To swap two numbers, say a,b , in Python, you can use assignment operator as shown below:
a,b = b,a
Thats it. Now
b holds the original value of
a holds the original value of
b. Let use see an example.
Example 1: Swap two Numbers
In this example, we take two variables holding numbers. Then we swap the data in those variables.
a = 5 b = 45 a,b = b,a print('a:',a) print('b:',b)
a: 45 b: 5
In this tutorial of Python Examples, we learned how to swap two numbers, using assignment operator.
- How to Get Number of Axes in Pandas DataFrame?
- Python – Factorial of a Number
- Python – Sum of Two Numbers
- Python Program to Add Two Numbers
- Python – Check if Number is Armstrong
- Python – Sum of First N Natural Numbers
- Python – Smallest of Three Numbers
- Python String – Find the number of overlapping occurrences of a substring
- Python – Largest of Three Numbers
- Reverse a Number in Python