• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Swap Two Values of Variables in Python

Swap Two Values of Variables in Python

February 25, 2022 Leave a Comment

With Python, we can easily swap two values between variables. The easiest way is to use tuple unpacking.

x = 2
y = 3

x, y = y, x

print(x)
print(y)

#Output:
3
2

You can also use a temporary variable to swap the values of two variables.

x = 2
y = 3

temp_var = x
x = y
y = temp_var

print(x)
print(y)

#Output:
3
2

When working with variables in Python, being able to change the values of variables easily is important.

One such change is swapping the values between two variables.

We can easily swap values of two variables in Python. To swap values you can use a temporary variable, or the easiest way is to swap values is with tuple unpacking.

Below is an example in Python of how to swap the values of two variables using tuple unpacking.

x = 2
y = 3

x, y = y, x

print(x)
print(y)

#Output:
3
2

Swap Values in Python with Temporary Variable

We can also swap values in Python with the use of a temporary variable.

In this method, we first store the first value in the temporary variable, set the first variable equal to the second variable, and then set the second variable equal to the temporary variable.

After swapping the values, we won’t be using the temporary variable, making this method less optimal than the method of tuple unpacking.

Below is how to swap two variables in Python with a temporary variable.

x = 2
y = 3

temp_var = x
x = y
y = temp_var

print(x)
print(y)

#Output:
3
2

How to Swap Two Items in a List Using Python

We can also swap items in a list using Python using the same tuple unpacking method of how to swap two variables.

Below is an example in Python of how to swap two items in a list.

def swapPositions(lst,position1, position2):
    lst[position1], lst[position2] = lst[position2], lst[position1]
    return lst

print(swapPositions([0,1,2,3,4,5],2,3))

#Output:
[0,1,3,2,4,5]

Hopefully this article has been useful for you to learn how to swap two values with Python.

Other Articles You'll Also Like:

  • 1.  Get Random Letter in Python
  • 2.  Using Python to Add Trailing Zeros to String
  • 3.  Using Python to Sum the Digits of a Number
  • 4.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 5.  Using Python to Get Domain from URL
  • 6.  Using Python to Remove Quotes from String
  • 7.  Python Remove First Element from List
  • 8.  Python Check if Object Has Attribute
  • 9.  Python sin – Find Sine of Number in Radians Using math.sin()
  • 10.  How to Create Array from 1 to n in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy