Swapping variable values also referred as swapping variables.
To swap variables, we need to follow the below steps:
- Assign x to a temporary variable say suppose
temp = x - Assign
ytoxas we backup ofxintemp, so if value ofxis changed we still have it intemp. Sox = y. - Assign
temptoyas we already have backup of old value ofxintemp, so weโll update the value ofywithtempi.e an old value ofx. Soy = 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
Related Articles
Deepen your understanding with these curated continuations.
How to Display Multiplication Tables in Python
Learn to generate multiplication tables in Python using for loops and the range() function. A perfect exercise for beginners to master iterative logic.
How to Generate Random Numbers in Python: A Quick Guide
Learn how to generate random integers and floats in Python using the random library. Master randint() and random() functions with simple code examples.
How to Check if a Variable is an Integer in Python
Learn how to check if a variable is an integer in Python using the type() and isinstance() functions. Essential for data validation and type-safe coding.