
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to Swap Two Variables using Python?
By using a temporary variable −
>>> x=10 >>> y=20 >>> z=x >>> x=y >>> y=z >>> x,y (20, 10)
Without using temporary variable
>>> a,b=5,7 >>> a,b (5, 7) >>> a,b=b,a >>> a,b (7, 5)
- Related Articles
- Swap two variables in one line in using Python?
- How to swap two variables in JavaScript?
- Swap two variables in one line using C#
- How to swap two String variables without third variable.
- Swap two variables in one line in using Java
- How to swap variables using Destructuring Assignment in JavaScript?
- Swap two variables in one line in Java
- How to remove swap files using Python?
- Swap two variables in one line in C/C++, Python, PHP and Java
- How to swap variables with destructuring in JavaScript?
- How to compare two variables in an if statement using Python?
- Swap two variables in one line in C/C+
- How to swap two numbers without using a temp variable in C#
- How to swap two arrays without using temporary variable in C language?
- Java program to swap two numbers using XOR operator

Advertisements