Title: Swapping variable values
Slug: swapping_variable_values
Summary: Swapping variable values
Date: 2016-05-01 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Setup the originally variables and their values


In [1]:
one = 1
two = 2

View the original variables


In [2]:
'one =', one, 'two =', two


Out[2]:
('one =', 1, 'two =', 2)

Swap the values


In [3]:
one, two = two, one

View the swapped values, notice how the values for each variable have changed


In [4]:
'one =', one, 'two =', two


Out[4]:
('one =', 2, 'two =', 1)