Worked Examples · Example 10.2
Q.Example 10.2: Tuple assignment — assign respective values from a tuple on the right side to a tuple of variables on the left side: (num1,num2) = (10,20); unpack record = ("Pooja",40,"CS") into (name,rollNo,subject); and show that (a,b,c,d) = (5,6,8) raises ValueError: not enough values to unpack (expected 4, got 3).
Tripura TbseTextbookSubjective· 2mImportance★★★★★est
10% · 3/30 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Tuple assignment (unpacking) pairs right-side values with left-side names strictly by position, and the counts must match — 4 names for 3 values raises ValueError: not enough values to unpack.
The idea. Python allows a tuple of variables on the left of = and a tuple of values on the right; it then unpacks them positionally. This is how a single record like ("Pooja", 40, "CS") is split into meaningful named parts in one statement — no indexing needed. The safety rule is strict: exactly one value per variable.
Program (as printed in the example):
(num1, num2) = (10, 20) # values assigned position by position
print("num1:", num1)
print("num2:", num2)
record = ("Pooja", 40, "CS")
(name, rollNo, subject) = record # unpacking a record
print("name:", name)
print("rollNo:", rollNo)
print("subject:", subject)
(a, b, c, d) = (5, 6, 8) # 4 variables but only 3 values -> ValueError
Verified run:
num1: 10
num2: 20
name: Pooja
rollNo: 40
subject: CS
Traceback (most recent call last):
File "test.py", line 11, in <module>
(a, b, c, d) = (5, 6, 8) # 4 variables but only 3 values -> ValueError
^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 4, got 3)
What happened:
(num1, num2) = (10, 20)— position 1 tonum1, position 2 tonum2.(name, rollNo, subject) = record— unpacking a 3-field record into three named variables; order in the tuple defines meaning. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.