# Sorting Algorithms - Swapping Function, and Swapping Variables in General

I just wanted to share, you can swap variables without using a temporary variable, just by using a, b = b, a.

Refer to this Screen Link: Learn data science with Python and R projects

``````a = 6
b = 4

a = b
b = a

print('a: {0}, b: {1}, {2}'.format(a, b, 'Incorrect!'))
Output: a: 4, b: 4, Incorrect!
``````
``````a = 6
b = 4

temp = a
a = b
b = temp

print('a: {0}, b: {1}, {2}'.format(a, b, 'Correct!'))
Output: a: 4, b: 6, Correct!
``````
``````a = 6
b = 4

a, b = b, a

print('a: {0}, b: {1}, {2}'.format(a, b, 'Correct!'))
Output: a: 4, b: 6, Correct!
``````

So in the context of the exercise, this function does not swap as intended:

``````def bad_swap(values, i, j):
values[i] = values[j]
values[j] = values[i]
``````

But this one does:

``````def swap(values, i, j):
values[i], values[j] = values[j], values[i]
``````

@semenchuk.ivan Yep. This is useful and available in python since python uses passing by value. Take note this is not possible with other programming languages like C, C++ or Java which are passed by reference (requires a temp variable for storage).

https://courses.washington.edu/css342/zander/css332/passby.html