Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Effortlessly Swap Two Python Variables Without a Temporary Variable

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

  1. Python Variables, Constants, and Literals – A Comprehensive Guide
  2. Understanding Python Global, Local, and Nonlocal Variables
  3. Python Print Without Newline: Mastering the end Parameter & Other Techniques
  4. How to Rename Files and Directories in Python with os.rename() – Step-by-Step Guide
  5. Automating Facebook Login with Python and Selenium: A Step‑by‑Step Guide
  6. Calculating Averages in Python: A Practical Guide
  7. Convert Strings to Title Case with Python – Fast & Reliable
  8. Python Variable Types: Understanding and Using Data Types
  9. Send Email in Python Using SMTP: Quick & Easy Guide
  10. Build a Real-Time Face-Tracking System with Arduino & OpenCV