Python Data Science Jobs & Interviews
20.4K subscribers
188 photos
4 videos
25 files
326 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
Python Tip: Tuple Unpacking for Multiple Assignments

Assigning multiple variables at once from a sequence can be done elegantly using tuple unpacking (also known as sequence unpacking). It's clean and efficient.

Traditional way:
coordinates = (10, 20)
x = coordinates[0]
y = coordinates[1]
print(f"X: {x}, Y: {y}")


Using Tuple Unpacking:
coordinates = (10, 20)
x, y = coordinates
print(f"X: {x}, Y: {y}")


This also works with lists and functions that return multiple values. It's often used for swapping variables without a temporary variable:

a = 5
b = 10
a, b = b, a # Swaps values of a and b
print(f"a: {a}, b: {b}") # Output: a: 10, b: 5


#PythonTip #TupleUnpacking #Assignment #Pythonic #Coding
---
By: @DataScienceQ