In Python, packing and unpacking are powerful features that allow you to work with multiple values easily. They help you assign, pass, and extract data in a clean and flexible way.
These concepts are widely used with:
- Tuples
- Lists
- Functions (*args and **kwargs)
- Data handling
🔹 What is Packing in Python?
Packing means:
Combining multiple values into a single variable.
Python automatically packs values into a tuple when needed.
🔹 Example of Packing
data = 10, 20, 30
print(data)
🔸 Output:
(10, 20, 30)
🔍 Explanation:
- Multiple values are packed into a tuple
- No brackets needed
🔹 Packing in Variables
a = 1
b = 2
c = 3
packed = a, b, c
print(packed)
🔹 What is Unpacking in Python?
Unpacking means:
Extracting values from a collection into separate variables.
🔹 Example of Unpacking
data = (10, 20, 30)
a, b, c = data
print(a)
print(b)
print(c)
🔸 Output:
10
20
30
🔹 Unpacking Lists
numbers = [1, 2, 3]
x, y, z = numbers
print(x, y, z)
🔹 Packing and Unpacking Together
a, b, c = 100, 200, 300
print(a, b, c)
🔍 Explanation:
- Right side → packing
- Left side → unpacking
🔹 Using * (Asterisk) for Unpacking
Python allows flexible unpacking using *.
🔹 Example: Star Unpacking
numbers = [1, 2, 3, 4, 5]
a, *b, c = numbers
print(a)
print(b)
print(c)
🔸 Output:
1
[2, 3, 4]
5
🔹 Explanation of Star Unpacking
| Variable | Value |
|---|---|
| a | First item |
| b | Middle items |
| c | Last item |
🔹 Swapping Variables Using Unpacking
a = 10
b = 20
a, b = b, a
print(a, b)
🔸 Output:
20 10
🔹 Unpacking in Functions (*args)
def show(*args):
print(args)
show(1, 2, 3, 4)
🔸 Output:
(1, 2, 3, 4)
🔹 Unpacking in Function Calls
def add(a, b, c):
print(a + b + c)
numbers = (10, 20, 30)
add(*numbers)
🔸 Output:
60
🔹 Dictionary Unpacking (**kwargs)
def info(**kwargs):
print(kwargs)
info(name="Alex", age=25)
🔸 Output:
{'name': 'Alex', 'age': 25}
🔹 Unpacking Dictionary into Function
def user(name, age):
print(name, age)
data = {"name": "Sophy", "age": 22}
user(**data)
🔸 Output:
Sophy 22
🔹 Real-Life Example: Student Data
student = ("John", 20, "Computer Science")
name, age, course = student
print(name)
print(age)
print(course)
🔹 Real-Life Example: API Data Simulation
user = {"id": 101, "name": "Anna", "role": "admin"}
def show_user(id, name, role):
print(id, name, role)
show_user(**user)
🔹 Packing vs Unpacking
| Feature | Packing | Unpacking |
|---|---|---|
| Meaning | Combine values | Split values |
| Symbol | None / *args | * or ** |
| Result | Single variable | Multiple variables |
🔹 Why Use Packing and Unpacking?
✔ Makes code cleaner
✔ Reduces number of variables
✔ Improves function flexibility
✔ Helps in data handling
✔ Widely used in real projects
🔹 Common Mistakes
❌ Too few variables:
a, b = (1, 2, 3)
❌ Too many variables:
a, b, c = (1, 2)
✔ Correct way:
a, *b = (1, 2, 3)
🚀 Conclusion
Python packing and unpacking are powerful tools that make working with multiple values simple and efficient. They are widely used in functions, APIs, and real-world applications.
Once you master this concept, you can write:
- Cleaner code
- More flexible functions
- Better data handling logic


0 Comments