Swapping Variable Values in Python

Swapping variable values also referred as swapping variables.

To swap variables, we need to follow the below steps:

  1. Assign x to a temporary variable say suppose temp = x
  2. Assign y to x as we backup of x in temp, so if value of x is changed we still have it in temp. So x = y.
  3. Assign temp to y as we already have backup of old value of x in temp, so we’ll update the value of y with temp i.e an old value of x. So y = temp

In this article we’ll see how to swap values. Let us understand also with an example.

Logic to manually swap values in 2 variables

temp = x

x = y

y = temp

But here we’ve to use third variable to swap variables.

Program for swapping values in 2 variables

# Swapping values

x = input('Enter value for x: ')
y = input('Enter value for y: ')

print('---- Before Swapping ----')
print('X:', x)
print('Y:', y)

# Swapping logic

temp = x    # storing x in temp
x = y       # storing y in x
y = temp    # storing temp in y

print('---- After Swapping ----')
print('X:', x)
print('Y:', y)

Output

Enter value for x: Hello
Enter value for y: World
---- Before Swapping ----
X: Hello
Y: World
---- After Swapping ----
X: World
Y: Hello

Shorter way to swap values

  • Python also provides a shorter way to swap variables.
  • We need to pass comma separated operands(value, variable or expression)
  • Python also allows to swap more than 2 operands.

Syntax

x, y = y, x

Program for swapping values in shorter way

# Swapping Values

x = input('Enter value for x: ')
y = input('Enter value for y: ')

print('---- Before Swapping ----')
print('X:', x)
print('Y:', y)

# Swapping
x, y = y, x

print('---- After Swapping ----')
print('X:', x)
print('Y:', y)

Output

Enter value for x: 24
Enter value for y: 12
---- Before Swapping ----
X: 24
Y: 12
---- After Swapping ----
X: 12
Y: 24

Program for swapping more than 2 values in shorter way

# Swapping Values

x = input('Enter value for x: ')
y = input('Enter value for y: ')
z = input('Enter value for z: ')

print('---- Before Swapping ----')
print('X:', x)
print('Y:', y)
print('Z:', z)

# Swapping
x, y, z = z, x, y

print('---- After Swapping ----')
print('X:', x)
print('Y:', y)
print('Z:', z)

Here x, y, z = z, x, y will swap value in specified order, i.e at the last x will have old value of z, y will have old value of x and z will have old value of y

Output

Enter value for x: 24.12
Enter value for y: 100.2
Enter value for z: 57.6
---- Before Swapping ----
X: 24.12
Y: 100.2
Y: 57.6
---- After Swapping ----
X: 57.6
Y: 24.12
Z: 100.2

Program for swapping with static value

# Swapping Values

x = 8

print('---- Before Swapping ----')
print('X:', x)

# Swapping
x, y = 45, x

print('---- After Swapping ----')
print('X:', x)
print('Y:', y)

Output

---- Before Swapping ----
X: 8
---- After Swapping ----
X: 45
Y: 8

Hope you like this!

Keep helping and happy 😄 coding

Vishnu Damwala
Vishnu Damwala

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳