The Curious Case of Swapping Variables in Python
📰 Medium · Python
From temp variables and XOR hacks to the elegant mechanics of Python’s one-liner. Continue reading on Medium »
Full Article
Title: The Curious Case of Swapping Variables in Python
URL Source: https://medium.com/@sreyas-unni/the-curious-case-of-swapping-variables-in-python-94fb2a92ca23?source=rss------python-5
Published Time: 2026-06-17T14:30:56Z
Markdown Content:
[Sitemap](https://medium.com/sitemap/sitemap.xml)
[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)
[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)
Get app
[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)
[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

# The Curious Case of Swapping Variables in Python
[](https://medium.com/@sreyas-unni?source=post_page---byline--94fb2a92ca23---------------------------------------)
[Sherlock](https://medium.com/@sreyas-unni?source=post_page---byline--94fb2a92ca23---------------------------------------)
Follow
3 min read
·
Jun 17, 2026
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2F94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&user=Sherlock&userId=06826f717cc7&source=---header_actions--94fb2a92ca23---------------------clap_footer------------------)
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Frepost%2Fp%2F94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&user=Sherlock&userId=06826f717cc7&source=---header_actions--94fb2a92ca23---------------------repost_header------------------)
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2F94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=---header_actions--94fb2a92ca23---------------------bookmark_footer------------------)
[Listen](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2Fplans%3Fdimension%3Dpost_audio_button%26postId%3D94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=---header_actions--94fb2a92ca23---------------------post_audio_button------------------)
Share
From temp variables and XOR hacks to the elegant mechanics of Python’s one-liner.

Swapping the values of two variables is one of the very first logic puzzles we learn in programming. In most languages, it requires a bit of gymnastics. But in Python, it feels like pure magic:
a, b = b, a
Have you ever stopped to wonder _how_ this actually works without destroying the data along the way? Today, let’s put on our detective hats and investigate the four different ways to solve the swap puzzle — and see why Python’s one-liner
URL Source: https://medium.com/@sreyas-unni/the-curious-case-of-swapping-variables-in-python-94fb2a92ca23?source=rss------python-5
Published Time: 2026-06-17T14:30:56Z
Markdown Content:
[Sitemap](https://medium.com/sitemap/sitemap.xml)
[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)
[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)
Get app
[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)
[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

# The Curious Case of Swapping Variables in Python
[](https://medium.com/@sreyas-unni?source=post_page---byline--94fb2a92ca23---------------------------------------)
[Sherlock](https://medium.com/@sreyas-unni?source=post_page---byline--94fb2a92ca23---------------------------------------)
Follow
3 min read
·
Jun 17, 2026
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2F94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&user=Sherlock&userId=06826f717cc7&source=---header_actions--94fb2a92ca23---------------------clap_footer------------------)
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Frepost%2Fp%2F94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&user=Sherlock&userId=06826f717cc7&source=---header_actions--94fb2a92ca23---------------------repost_header------------------)
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2F94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=---header_actions--94fb2a92ca23---------------------bookmark_footer------------------)
[Listen](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2Fplans%3Fdimension%3Dpost_audio_button%26postId%3D94fb2a92ca23&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40sreyas-unni%2Fthe-curious-case-of-swapping-variables-in-python-94fb2a92ca23&source=---header_actions--94fb2a92ca23---------------------post_audio_button------------------)
Share
From temp variables and XOR hacks to the elegant mechanics of Python’s one-liner.

Swapping the values of two variables is one of the very first logic puzzles we learn in programming. In most languages, it requires a bit of gymnastics. But in Python, it feels like pure magic:
a, b = b, a
Have you ever stopped to wonder _how_ this actually works without destroying the data along the way? Today, let’s put on our detective hats and investigate the four different ways to solve the swap puzzle — and see why Python’s one-liner
DeepCamp AI