Tuple Packing, Unpacking and Tuple Immuntability Rules Notes in Hindi & English : टपल अनपैकिंग (Unpacking), स्वैपिंग (a, b = b, a) और सुरक्षा और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Tuple Packing, Unpacking and Tuple Immuntability Rules (टपल अनपैकिंग (Unpacking), स्वैपिंग (a, b = b, a) और सुरक्षा) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Tuple packing groups comma-separated values into a tuple automatically, while unpacking extracts them into target variables in one statement. Python leverages implicit tuple packing and unpacking for temporary-free variable swapping (a, b = b, a), extended *star capture, and shallow immutability enforcement.

अध्याय सार: टपल पैकिंग कॉमा से अलग मानों को स्वतः एक टपल में बांधती है, जबकि अनपैकिंग एक ही स्टेटमेंट में उन्हें अलग-अलग वेरिएबल्स में निकालती है। पायथन इसी तंत्र से बिना अस्थायी वेरिएबल के स्वैपिंग (a, b = b, a), *स्टार अनपैकिंग और शालो इम्यूटेबिलिटी प्रदान करता है।

Tuple Packing, Unpacking and Tuple Immuntability Rules: Detailed Notes & Architecture (टपल अनपैकिंग (Unpacking), स्वैपिंग (a, b = b, a) और सुरक्षा)

1. Tuple Packing and Exact Unpacking Rules (टपल पैकिंग और सटीक अनपैकिंग के नियम)

[EN] Tuple packing occurs whenever multiple comma-separated expressions appear on the right-hand side of an assignment or return statement, e.g., record = 'Raj', 25, 'Jaipur'. Tuple unpacking (sequence unpacking) assigns each element positionally to matching targets on the left: name, age, city = record. Without a starred target, the number of variables on the left must match the tuple's length exactly; otherwise Python raises ValueError: not enough values to unpack or too many values to unpack.

[HI] जब भी असाइनमेंट या return स्टेटमेंट के दाईं ओर कॉमा से अलग मान लिखे जाते हैं जैसे record = 'Raj', 25, 'Jaipur', तो टपल पैकिंग होती है। अनपैकिंग उन्हें क्रमशः बाईं ओर के वेरिएबल्स में बांट देती है: name, age, city = record। यदि बाईं ओर वेरिएबल्स की संख्या टपल की लंबाई से कम या अधिक हो (बिना * के), तो ValueError उत्पन्न होता है।

2. Simultaneous Swapping via a, b = b, a (a, b = b, a द्वारा त्वरित वेरिएबल स्वैपिंग)

[EN] In Python, a, b = b, a swaps two variables cleanly without a temporary variable. Python always evaluates the right-hand side completely first, packing the current values of b and a into an implicit tuple (b, a), and then unpacks that tuple from left to right into targets a and b. This guarantees zero overwrite hazards, even in multi-variable shifts like a, b, c = b, c, a or Fibonacci updates a, b = b, a + b.

[HI] पायथन में a, b = b, a बिना किसी तीसरे अस्थायी वेरिएबल के दोनों मानों की अदला-बदली (swapping) कर देता है। पायथन पहले दाईं ओर के पूरे एक्सप्रेशन का मूल्यांकन कर उसे एक अंतर्निहित टपल (b, a) में पैक करता है, और फिर बाएं से दाएं क्रम में a और b में अनपैक करता है। इसी कारण फिबोनाची अपडेट a, b = b, a + b भी बिना डेटा खोए सही काम करता है।

3. Extended Iterable Unpacking with * (PEP 3132) (* ऑपरेटर द्वारा एक्सटेंडेड अनपैकिंग (PEP 3132))

[EN] Python 3 supports placing a single starred target (*rest) on the left-hand side to capture zero or more leftover items: first, *middle, last = (10, 20, 30, 40) binds middle to [20, 30]. Crucially, the starred variable ALWAYS receives a list—even when unpacking a tuple or when zero elements remain (empty list []). Using more than one starred expression in the same assignment level (like *a, *b = t) raises a SyntaxError.

[HI] पायथन 3 में बाईं ओर एक *स्टार वाला वेरिएबल (*rest) लिखकर बचे हुए शून्य या अधिक मान इकट्ठे किए जा सकते हैं: first, *middle, last = (10, 20, 30, 40) में middle का मान [20, 30] होगा। ध्यान रहे, * वाला वेरिएबल हमेशा एक list ही प्राप्त करता है (टपल नहीं), भले ही बचे हुए मान शून्य हों ([] )। एक ही स्तर पर दो * वेरिएबल (जैसे *a, *b = t) लिखने पर SyntaxError आता है।

4. Immutability Boundaries and Security Guarantees (इम्यूटेबिलिटी की सीमाएं और डेटा सुरक्षा)

[EN] Tuple immutability protects container structure: elements cannot be added, deleted (del t[0] raises TypeError), or rebound (t[0] = 99 raises TypeError), preventing accidental side effects across function boundaries. However, immutability is shallow—reference slots are frozen, not the mutable objects those slots might point to. Thus, t = ([1], 2); t[0] += [2] actually mutates the inner list AND then raises TypeError when trying to rebind t[0]!

[HI] टपल की इम्यूटेबिलिटी संरचनात्मक सुरक्षा देती है: कोई नया तत्व जोड़ा नहीं जा सकता, del t[0] या t[0] = 99 लिखने पर TypeError आता है। हालांकि यह इम्यूटेबिलिटी शालो (shallow) होती है—अर्थात रेफरेंस स्लॉट स्थिर रहते हैं, परंतु यदि कोई स्लॉट किसी म्यूटेबल लिस्ट की ओर इशारा करे तो उसके अंदर बदलाव हो सकता है। एक प्रसिद्ध अपवाद t = ([1], 2); t[0] += [2] है, जो आंतरिक लिस्ट को बदल भी देता है और फिर t[0] को पुनः असाइन करने के प्रयास में TypeError भी देता है।

Syntax, Formula & Technical Example (Tuple Packing, Unpacking and Tuple Immuntability Rules)

a, b = 10, 20
a, b = b, a                        # swaps using implicit tuple (20, 10)
first, *mid, last = (1, 2, 3, 4)  # mid is [2, 3] (always a list!)
x, *tail = (99,)                  # x=99, tail=[]
print(a, b, mid, tail)            # 20 10 [2, 3] []
t = (5, 6); old_id = id(t)
t += (7,)                         # new tuple object created
print(t, id(t) == old_id)         # (5, 6, 7) False

Tuple Packing, Unpacking and Tuple Immuntability Rules Quick Reference & Comparison Table

Packing / Unpacking FormResulting Variables & TypesKey Exam Fact
t = 1, 2, 3t is tuple (1, 2, 3)Implicit tuple packing without parentheses
a, b = b, aSwaps values of a and bRHS packs (b, a) first, then unpacks left to right
h, *t = (1, 2, 3)h is 1; t is list [2, 3]Starred target ALWAYS produces a list, never a tuple
x, y = (1, 2, 3)Raises ValueErrorTarget count must match unless * target is used

Tuple Packing, Unpacking and Tuple Immuntability Rules Top 20 Bilingual MCQs with Answers

Q1. What type does the starred variable 'mid' have after a, *mid, b = (10, 20, 30, 40)?

(a, *mid, b = (10, 20, 30, 40) चलाने के बाद 'mid' का डेटा टाइप क्या होगा?)

(A) tuple

(B) list

(C) int

(D) set

Correct Answer: (B)

[EN] Extended iterable unpacking (PEP 3132) always assigns a list to the starred target, even when unpacking a tuple.

[HI] एक्सटेंडेड अनपैकिंग (PEP 3132) में * वाला वेरिएबल टपल को अनपैक करने पर भी हमेशा list ही प्राप्त करता है।

Q2. What happens when you execute x, y = (1, 2, 3)?

(x, y = (1, 2, 3) चलाने पर क्या होता है?)

(A) Raises ValueError

(B) x=1, y=2 (3 is ignored)

(C) Raises TypeError

(D) x=1, y=(2, 3)

Correct Answer: (A)

[EN] Having fewer targets than values without a starred variable raises ValueError: too many values to unpack (expected 2).

[HI] बिना * के कम वेरिएबल्स देने पर ValueError: too many values to unpack उत्पन्न होता है।

Q3. In Python, how does a, b = b, a execute internally without a temporary variable?

(पायथन में a, b = b, a बिना अस्थायी वेरिएबल के आंतरिक रूप से कैसे काम करता है?)

(A) Right-hand side is evaluated and packed into a tuple, then unpacked into left targets

(B) Both variables share the same memory address permanently

(C) Left side is evaluated before right side

(D) Python swaps pointers inside the CPU accumulator

Correct Answer: (A)

[EN] Python evaluates the RHS tuple (b, a) completely first, then assigns element 0 to a and element 1 to b.

[HI] पायथन पहले दाईं ओर का पूरा टपल (b, a) तैयार करता है और फिर उसे क्रमशः बाईं ओर के a और b में अनपैक करता है।

Q4. What are a and b after a, b = 0, 1; a, b = b, a + b; a, b = b, a + b?

(a, b = 0, 1; a, b = b, a + b; a, b = b, a + b के बाद a और b का मान क्या होगा?)

(A) 1 and 2

(B) 1 and 1

(C) 2 and 3

(D) 1 and 3

Correct Answer: (A)

[EN] Step 1: a=1, b=1. Step 2: RHS evaluates (1, 1+1) = (1, 2), so a=1 and b=2.

[HI] पहले चरण में a=1, b=1। दूसरे चरण में दायां पक्ष (1, 1+1)=(1, 2) बनता है, अतः a=1 और b=2 होता है।

Q5. What is the value of rest after first, *rest = (42,)?

(first, *rest = (42,) चलाने के बाद rest का मान क्या होगा?)

(A) ()

(B) None

(C) ValueError

(D) []

Correct Answer: (D)

[EN] first receives 42 and the starred target captures zero remaining elements as an empty list [].

[HI] first को 42 मिलता है और बचे हुए शून्य तत्वों के लिए *rest को खाली लिस्ट [] मिलती है।

Q6. What happens when you write *a, *b = (1, 2, 3, 4)?

(*a, *b = (1, 2, 3, 4) लिखने पर क्या होता है?)

(A) a=[1, 2, 3], b=[4]

(B) a=[1, 2], b=[3, 4]

(C) ValueError

(D) SyntaxError

Correct Answer: (D)

[EN] Python grammar allows at most one starred expression in a single unpacking target list; two cause SyntaxError.

[HI] एक अनपैकिंग स्टेटमेंट में एक से अधिक * वेरिएबल लिखने की अनुमति नहीं है, जिससे SyntaxError आता है।

Q7. What happens when executing t = (1, 2, 3); del t[0]?

(t = (1, 2, 3); del t[0] चलाने पर क्या होता है?)

(A) Raises ValueError

(B) Raises IndexError

(C) Raises TypeError

(D) t becomes (2, 3)

Correct Answer: (C)

[EN] Individual elements cannot be deleted from an immutable tuple (TypeError: 'tuple' object doesn't support item deletion), though del t deletes the whole variable.

[HI] इम्यूटेबल टपल के किसी एक तत्व को del t[0] से नहीं हटाया जा सकता (TypeError आता है), केवल पूरा वेरिएबल del t से हटता है।

Q8. What does def f(): return 10, 20; res = f() assign to res?

(def f(): return 10, 20; res = f() लिखने पर res को क्या मान मिलता है?)

(A) The tuple (10, 20)

(B) SyntaxError

(C) The list [10, 20]

(D) 10 only

Correct Answer: (A)

[EN] Returning comma-separated expressions packs them into a single tuple (10, 20) automatically.

[HI] फंक्शन से कॉमा द्वारा अलग किए गए मान लौटाने पर वे स्वतः एक टपल (10, 20) में पैक होकर लौटते हैं।

Q9. What is the output of (a, (b, c)) = (1, (2, 3)); print(a, b, c)?

((a, (b, c)) = (1, (2, 3)); print(a, b, c) का आउटपुट क्या होगा?)

(A) 1 2 3

(B) SyntaxError

(C) 1 (2, 3) None

(D) ValueError

Correct Answer: (A)

[EN] Nested tuple unpacking matches the nested structure directly, binding a=1, b=2, and c=3.

[HI] नेस्टेड टपल अनपैकिंग आंतरिक संरचना का मिलान करके सीधे a=1, b=2 और c=3 असाइन कर देती है।

Q10. What happens when running t = ([1, 2], 3); t[0] += [4]?

(t = ([1, 2], 3); t[0] += [4] चलाने पर क्या होता है?)

(A) Nothing changes and TypeError is raised

(B) t becomes ([1, 2, 4], 3) with no error

(C) SyntaxError

(D) t[0] becomes [1, 2, 4] AND TypeError is raised

Correct Answer: (D)

[EN] list += mutates [1, 2] to [1, 2, 4] in place and returns the list, then Python tries to store it back into t[0] which fails with TypeError!

[HI] पहले list += लिस्ट को [1, 2, 4] में बदलता है, लेकिन उसके बाद परिणाम को वापस t[0] में असाइन करते समय टपल TypeError दे देता है।

Q11. What does x, _, z = ('A', 'ignore', 'C'); print(x, z) output?

(x, _, z = ('A', 'ignore', 'C'); print(x, z) का आउटपुट क्या होगा?)

(A) A ignore

(B) A C

(C) ValueError

(D) SyntaxError

Correct Answer: (B)

[EN] By Python convention, the underscore _ acts as a valid throwaway variable name during tuple unpacking.

[HI] पायथन में अंडरस्कोर _ एक मान्य वेरिएबल नाम है जिसे अनपैकिंग में अनावश्यक मान छोड़ने के लिए उपयोग किया जाता है।

Q12. Can tuple unpacking unpack non-tuple iterables like a string or list, e.g., a, b, c = 'XYZ'?

(क्या अनपैकिंग द्वारा स्ट्रिंग या लिस्ट जैसे अन्य इटरेबल भी अनपैक किए जा सकते हैं, जैसे a, b, c = 'XYZ'?)

(A) It raises TypeError

(B) Yes, any iterable of matching length can be unpacked

(C) Only lists and tuples, not strings

(D) No, only tuples can be unpacked

Correct Answer: (B)

[EN] Tuple unpacking in Python is actually general iterable unpacking and works on lists, strings, sets, dicts, and generators.

[HI] हाँ, अनपैकिंग वास्तव में किसी भी इटरेबल (लिस्ट, स्ट्रिंग, सेट, जनरेटर) पर काम करती है।

Q13. What happens if you run *x = (1, 2, 3) without any non-starred target?

(बिना किसी अन्य वेरिएबल के *x = (1, 2, 3) चलाने पर क्या होगा?)

(A) x becomes (1, 2, 3)

(B) SyntaxError

(C) x becomes [1, 2, 3]

(D) ValueError

Correct Answer: (B)

[EN] Python requires a starred assignment target to appear inside a tuple/list target (like *x, = (1, 2, 3)); bare *x = ... is a SyntaxError.

[HI] अकेले *x = ... लिखना SyntaxError है; टपल टार्गेट दर्शाने के लिए *x, = (1, 2, 3) लिखना आवश्यक होता है।

Q14. What is printed by t = (1, 2); u = t; t += (3,); print(u, t)?

(t = (1, 2); u = t; t += (3,); print(u, t) से क्या प्रिंट होगा?)

(A) TypeError

(B) (1, 2) (1, 2, 3)

(C) (1, 2, 3) (1, 2, 3)

(D) (1, 2) (1, 2)

Correct Answer: (B)

[EN] Because tuples are immutable, += cannot mutate t in place; it binds t to a new tuple (1, 2, 3) while u still points to (1, 2).

[HI] टपल इम्यूटेबल होने से += नया टपल (1, 2, 3) बनाकर t को देता है, जबकि u पुराने (1, 2) पर ही रहता है।

Q15. Which error is raised by a, b, c = (1, 2)?

(a, b, c = (1, 2) चलाने पर कौन-सी त्रुटि उत्पन्न होती है?)

(A) IndexError

(B) ValueError

(C) TypeError

(D) SyntaxError

Correct Answer: (B)

[EN] More targets (3) than tuple elements (2) triggers ValueError: not enough values to unpack (expected 3, got 2).

[HI] अधिक वेरिएबल (3) और कम तत्व (2) होने पर ValueError: not enough values to unpack आता है।

Q16. Which statement is NOT true about tuple packing, unpacking, and immutability?

(टपल पैकिंग, अनपैकिंग और इम्यूटेबिलिटी के बारे में कौन-सा कथन सत्य नहीं है?)

(A) a, b = b, a swaps values without a temporary variable

(B) Tuples prevent element reassignment with TypeError

(C) Extended unpacking with *rest stores remaining elements in a tuple

(D) Mutable objects nested inside a tuple can still be modified

Correct Answer: (C)

[EN] Extended unpacking with *rest ALWAYS collects remaining items into a list, not a tuple.

[HI] *rest द्वारा एक्सटेंडेड अनपैकिंग बचे हुए तत्वों को टपल में नहीं, बल्कि हमेशा एक list में संग्रहीत करती है।

Q17. Within Tuple Packing, Unpacking and Tuple Immuntability Rules, which packing / unpacking form directly maps to: "t is tuple (1, 2, 3)"?

(Tuple Packing, Unpacking and Tuple Immuntability Rules में कौन सा Packing / Unpacking Form सीधे "t is tuple (1, 2, 3)" से संबंधित है?)

(A) h, *t = (1, 2, 3)

(B) x, y = (1, 2, 3)

(C) t = 1, 2, 3

(D) a, b = b, a

Correct Answer: (C)

[EN] In Tuple Packing, Unpacking and Tuple Immuntability Rules, 't = 1, 2, 3' handles 't is tuple (1, 2, 3)' (Implicit tuple packing without parentheses).

[HI] Tuple Packing, Unpacking and Tuple Immuntability Rules में 't = 1, 2, 3' का कार्य/विवरण 't is tuple (1, 2, 3)' (Implicit tuple packing without parentheses) है।

Q18. Which option accurately describes 'a, b = b, a' in Tuple Packing, Unpacking and Tuple Immuntability Rules?

(Tuple Packing, Unpacking and Tuple Immuntability Rules के संदर्भ में 'a, b = b, a' की सही विशेषता कौन सी है?)

(A) Swaps values of a and b

(B) t is tuple (1, 2, 3)

(C) Raises ValueError

(D) h is 1; t is list [2, 3]

Correct Answer: (A)

[EN] 'a, b = b, a' provides 'Swaps values of a and b' — key point: RHS packs (b, a) first, then unpacks left to right.

[HI] 'a, b = b, a' का मुख्य विवरण 'Swaps values of a and b' है (RHS packs (b, a) first, then unpacks left to right)।

Q19. In Tuple Packing, Unpacking and Tuple Immuntability Rules, identify the correct parameter for the specification: "h is 1; t is list [2, 3]".

(Tuple Packing, Unpacking and Tuple Immuntability Rules में "h is 1; t is list [2, 3]" विवरण किस विकल्प के लिए सही है?)

(A) h, *t = (1, 2, 3)

(B) t = 1, 2, 3

(C) a, b = b, a

(D) x, y = (1, 2, 3)

Correct Answer: (A)

[EN] 'h, *t = (1, 2, 3)' corresponds to 'h is 1; t is list [2, 3]' (Starred target ALWAYS produces a list, never a tuple).

[HI] 'h, *t = (1, 2, 3)' का संबंध 'h is 1; t is list [2, 3]' (Starred target ALWAYS produces a list, never a tuple) से है।

Q20. Considering Tuple Packing, Unpacking and Tuple Immuntability Rules, what key behavior defines 'x, y = (1, 2, 3)'?

(Tuple Packing, Unpacking and Tuple Immuntability Rules में 'x, y = (1, 2, 3)' की मुख्य भूमिका या मान क्या है?)

(A) Raises ValueError

(B) h is 1; t is list [2, 3]

(C) Swaps values of a and b

(D) t is tuple (1, 2, 3)

Correct Answer: (A)

[EN] 'x, y = (1, 2, 3)' represents 'Raises ValueError' (Target count must match unless * target is used).

[HI] 'x, y = (1, 2, 3)' का अर्थ/मान 'Raises ValueError' है (Target count must match unless * target is used)।

Tuple Packing, Unpacking and Tuple Immuntability Rules — Exam FAQs

Q1: Why does Python's *rest unpacking return a list instead of a tuple even when unpacking a tuple?

टपल को अनपैक करते समय *rest एक टपल के बजाय लिस्ट क्यों लौटाता है?

[EN] By design in PEP 3132, the starred variable represents an arbitrary-length sequence of leftover items, and lists are Python's canonical variable-length homogeneous sequence type.

[HI] PEP 3132 के डिज़ाइन के अनुसार * वेरिएबल अज्ञात या परिवर्तनशील संख्या वाले तत्वों को संभालता है, और परिवर्तनीय लंबाई के लिए पायथन में list मानक डेटा प्रकार है।

Q2: Are tuples truly 'safe' from all modification if they hold lists or dictionaries?

यदि किसी टपल में लिस्ट या डिक्शनरी हो, तो क्या वह हर तरह के बदलाव से सुरक्षित रहता है?

[EN] No. Tuple immutability freezes only the top-level object references inside the tuple. Any mutable object referenced inside the tuple can still have its internal contents modified unless immutable replacements (tuples, frozensets) are used.

[HI] नहीं। टपल केवल अपने शीर्ष-स्तरीय रेफरेंस स्लॉट को स्थिर रखता है। अंदर रखी गई लिस्ट या डिक्शनरी के मान अभी भी बदले जा सकते हैं; पूर्ण सुरक्षा के लिए टपल के भीतर भी इम्यूटेबल प्रकार ही रखें।

Q3: How can I unpack only the first and last items of a 10-element tuple cleanly?

10 तत्वों वाले टपल से केवल पहला और अंतिम तत्व आसानी से कैसे अनपैक करें?

[EN] Use a throwaway starred target in the middle: first, *_, last = my_tuple. The middle eight elements are ignored in _ while first and last capture the endpoints.

[HI] बीच में थ्रोअवे स्टार वेरिएबल उपयोग करें: first, *_, last = my_tuple। इससे पहला और अंतिम मान सीधे मिल जाते हैं।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)