Effortlessly Swap Two Python Variables Without a Temporary Variable
With this neat little trick, you can swap two Python variables without using a third variable:
a = 1 b = 2 a, b = b, a print (a) # 2 print (b) # 1
It’s just one line of code! As you can see in line 3, no temporary variable is needed to swap variables in Python.
Python
- Python Variables, Constants, and Literals – A Comprehensive Guide
- Understanding Python Global, Local, and Nonlocal Variables
- Python Print Without Newline: Mastering the end Parameter & Other Techniques
- How to Rename Files and Directories in Python with os.rename() – Step-by-Step Guide
- Automating Facebook Login with Python and Selenium: A Step‑by‑Step Guide
- Calculating Averages in Python: A Practical Guide
- Convert Strings to Title Case with Python – Fast & Reliable
- Python Variable Types: Understanding and Using Data Types
- Send Email in Python Using SMTP: Quick & Easy Guide
- Build a Real-Time Face-Tracking System with Arduino & OpenCV