Python Tuples: Creation, Parentheses, Single Element Tuple (x,) Notes in Hindi & English : पायथन टपल (Tuple): गोल कोष्ठक, इम्यूटेबल प्रकृति और सिंगल टपल (5,) और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Tuples: Creation, Parentheses, Single Element Tuple (x,) (पायथन टपल (Tuple): गोल कोष्ठक, इम्यूटेबल प्रकृति और सिंगल टपल (5,)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Python tuples are ordered, indexed, immutable sequences constructed primarily by commas rather than parentheses alone. Creating a single-element tuple strictly requires a trailing comma such as (5,), because (5) simply evaluates as an integer.

अध्याय सार: पायथन टपल एक क्रमबद्ध, इंडेक्स्ड और इम्यूटेबल (अपरिवर्तनीय) सीक्वेंस डेटा टाइप है, जिसे कोष्ठक के बजाय मुख्यतः कॉमा (,) द्वारा बनाया जाता है। एकल-तत्व वाला टपल बनाने के लिए अंत में कॉमा लगाना अनिवार्य है जैसे (5,), क्योंकि (5) केवल एक पूर्णांक (int) माना जाता है।

Python Tuples: Creation, Parentheses, Single Element Tuple (x,): Detailed Notes & Architecture (पायथन टपल (Tuple): गोल कोष्ठक, इम्यूटेबल प्रकृति और सिंगल टपल (5,))

1. Tuple Creation and the Role of Commas (टपल निर्माण और कॉमा की भूमिका)

[EN] A tuple is an ordered collection capable of holding duplicate and heterogeneous elements, supporting positive/negative indexing and slicing in O(1) lookup time. Crucially, Python syntax defines tuples by the comma operator, not parentheses: writing t = 1, 2, 3 creates a valid tuple, whereas parentheses merely provide visual grouping or operator precedence. The sole exception where parentheses alone construct a tuple is the empty tuple (), or calling the tuple() constructor on an iterable such as tuple('abc').

[HI] टपल एक क्रमबद्ध संग्रह है जिसमें डुप्लिकेट और विभिन्न डेटा प्रकार के तत्व रखे जा सकते हैं, तथा इसमें इंडेक्सिंग और स्लाइसिंग काम करती है। महत्वपूर्ण बात यह है कि पायथन में टपल की पहचान गोल कोष्ठक () से नहीं बल्कि कॉमा (,) से होती है: t = 1, 2, 3 भी एक वैध टपल बनाता है। कोष्ठक का एकमात्र अपवाद खाली टपल () बनाना है, अथवा tuple() कंस्ट्रक्टर जैसे tuple('abc') से इटरेबल को टपल में बदलना है।

2. Single-Element Tuple Syntax (x,) vs (x) (सिंगल-एलिमेंट टपल सिंटैक्स (x,) बनाम (x))

[EN] A classic competitive-exam trap involves singleton tuples: writing t = (5) or s = ('hello') does NOT create a tuple—Python interprets the parentheses as arithmetic or grouping brackets, resulting in type int or str. To force Python's parser to construct a 1-element tuple, a trailing comma is mandatory: t = (5,) or t = 5,. Checking len((5,)) yields 1 and type((5,)) returns <class 'tuple'>.

[HI] परीक्षा में बार-बार पूछा जाने वाला प्रश्न सिंगल-एलिमेंट टपल है: t = (5) या s = ('hello') लिखने से टपल नहीं बनता, बल्कि पायथन इसे साधारण ग्रुपिंग मानकर int या str बना देता है। एक तत्व का टपल बनाने के लिए अंत में कॉमा (,) लगाना अनिवार्य है: t = (5,) या t = 5,। तभी len((5,)) का मान 1 और type((5,)) का प्रकार tuple होता है।

3. Memory Efficiency, Methods, and Immutability (मेमोरी दक्षता, मेथड्स और इम्यूटेबल प्रकृति)

[EN] Because tuples have fixed length after creation, CPython stores a PyTupleObject in a single contiguous memory block without the overallocation buffer used by lists, saving memory and enabling faster creation and small-tuple caching (freelist). Tuples expose only two public methods: count(x) and index(x), lacking append(), remove(), or sort(). Note that if a tuple holds a mutable object like t = ([1, 2], 3), you cannot reassign t[0], yet you can mutate the inner list via t[0].append(9).

[HI] टपल की लंबाई निर्माण के बाद निश्चित होती है, इसलिए CPython PyTupleObject को बिना अतिरिक्त बफर के एक ही मेमोरी ब्लॉक में संग्रहीत करता है, जिससे मेमोरी की बचत और तेज एक्सेस मिलता है। टपल में केवल दो ही मेथड count() और index() होते हैं; इसमें append() या remove() नहीं होते। यदि टपल के भीतर कोई म्यूटेबल लिस्ट हो जैसे t = ([1, 2], 3), तो t[0] बदला नहीं जा सकता पर t[0].append(9) द्वारा आंतरिक लिस्ट बदली जा सकती है।

4. Hashability and Usage as Dictionary Keys (हैशेबिलिटी और डिक्शनरी की (Key) के रूप में उपयोग)

[EN] A tuple is hashable—and therefore valid as a dictionary key or set element—if and only if every element inside it (recursively) is also hashable. For example, (10, 'coord', (1, 2)) works cleanly with hash() and as a dict key representing 2D coordinates. Conversely, a tuple containing a list or dict such as (1, [2, 3]) raises TypeError: unhashable type: 'list' when passed to hash() or inserted into a set.

[HI] कोई टपल तभी हैशेबल (hashable) होता है—अर्थात डिक्शनरी की key या सेट का तत्व बन सकता है—जब उसके भीतर मौजूद सभी तत्व भी स्वयं हैशेबल हों। उदाहरण के लिए, (10, 'coord', (1, 2)) को डिक्शनरी की बनाया जा सकता है। इसके विपरीत, यदि टपल के अंदर कोई लिस्ट या डिक्शनरी जैसे (1, [2, 3]) हो, तो hash() चलाने या सेट में डालने पर TypeError: unhashable type: 'list' आता है।

Syntax, Formula & Technical Example (Python Tuples: Creation, Parentheses, Single Element Tuple (x,))

empty = ()
single_int = (5)
single_tup = (5,)
packed = 10, 20, 10
print(type(single_int), type(single_tup)) # <class 'int'> <class 'tuple'>
print(packed.count(10), packed.index(20)) # 2 1
t = ([1, 2], 'ok'); t[0].append(3)
print(t)                                  # ([1, 2, 3], 'ok')

Python Tuples: Creation, Parentheses, Single Element Tuple (x,) Quick Reference & Comparison Table

Tuple ExpressionEvaluated Type & ValueKey Exam Fact
()tuple of length 0Only tuple syntax created without a comma
(5)int with value 5Parentheses alone act as grouping, NOT a tuple
(5,)tuple of length 1Trailing comma is mandatory for single-element tuple
1, 2, 3tuple (1, 2, 3)Commas without parentheses still pack into a tuple

Python Tuples: Creation, Parentheses, Single Element Tuple (x,) Top 20 Bilingual MCQs with Answers

Q1. What is the data type of t = (10) in Python?

(पायथन में t = (10) का डेटा टाइप क्या है?)

(A) int

(B) set

(C) tuple

(D) list

Correct Answer: (A)

[EN] Without a trailing comma, parentheses around 10 act solely as grouping brackets, making t an integer.

[HI] अंत में कॉमा न होने पर (10) के कोष्ठक केवल ग्रुपिंग का कार्य करते हैं, अतः t एक int है।

Q2. Which syntax creates a valid single-element tuple containing 42?

(42 मान वाला एकल-तत्व (single-element) टपल बनाने का सही सिंटैक्स कौन-सा है?)

(A) (42,)

(B) tuple(42)

(C) (42)

(D) [42,]

Correct Answer: (A)

[EN] A trailing comma is required: (42,) creates a 1-tuple, whereas tuple(42) raises TypeError because 42 is not iterable.

[HI] एकल-तत्व टपल के लिए अंत में कॉमा जरूरी है: (42,) सही है, जबकि tuple(42) में 42 इटरेबल न होने से TypeError आएगा।

Q3. How many public built-in methods does the tuple class have (excluding dunder methods)?

(डंडर मेथड्स को छोड़कर tuple क्लास में कितने पब्लिक बिल्ट-इन मेथड होते हैं?)

(A) 2

(B) 0

(C) 6

(D) 4

Correct Answer: (A)

[EN] Because tuples are immutable, they only provide count() and index().

[HI] टपल के इम्यूटेबल होने के कारण इसमें केवल दो मेथड count() और index() होते हैं।

Q4. What is the output of t = 1, 2, 3; print(type(t).__name__)?

(t = 1, 2, 3; print(type(t).__name__) का आउटपुट क्या होगा?)

(A) int

(B) SyntaxError

(C) tuple

(D) list

Correct Answer: (C)

[EN] Python packs comma-separated values into a tuple automatically even without parentheses.

[HI] बिना कोष्ठक के भी कॉमा से अलग किए गए मान स्वतः टपल में पैक हो जाते हैं।

Q5. What happens when running t = (1, 2, 3); t[1] = 9?

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

(A) Raises ValueError

(B) t becomes (1, 9, 3)

(C) Raises AttributeError

(D) Raises TypeError

Correct Answer: (D)

[EN] Tuples do not support item assignment and raise TypeError: 'tuple' object does not support item assignment.

[HI] टपल आइटम असाइनमेंट का समर्थन नहीं करता और TypeError उत्पन्न करता है।

Q6. What is the output of t = ([10, 20], 30); t[0].append(40); print(t)?

(t = ([10, 20], 30); t[0].append(40); print(t) का आउटपुट क्या होगा?)

(A) ([10, 20], 30)

(B) TypeError

(C) ([10, 20], 30, 40)

(D) ([10, 20, 40], 30)

Correct Answer: (D)

[EN] t[0] still references the exact same list object, whose internal contents can be mutated in place.

[HI] t[0] अभी भी उसी लिस्ट को संदर्भित करता है, इसलिए आंतरिक लिस्ट के भीतर append(40) सफलतापूर्वक हो जाता है।

Q7. Which of the following tuples CANNOT be used as a dictionary key?

(निम्न में से किस टपल को डिक्शनरी की (key) के रूप में उपयोग नहीं किया जा सकता?)

(A) (1, 2, 3)

(B) ()

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

(D) (1, [2, 3])

Correct Answer: (D)

[EN] (1, [2, 3]) contains a mutable, unhashable list, causing TypeError: unhashable type: 'list'.

[HI] (1, [2, 3]) में एक अनहैशेबल लिस्ट मौजूद है, जिससे TypeError आता है।

Q8. What does tuple('py') evaluate to?

(tuple('py') का परिणाम क्या होता है?)

(A) TypeError

(B) ('p', 'y')

(C) ('py',)

(D) ('py')

Correct Answer: (B)

[EN] tuple(iterable) iterates over 'py' character by character, constructing ('p', 'y').

[HI] tuple() कंस्ट्रक्टर स्ट्रिंग 'py' के प्रत्येक अक्षर पर इटरेट करके ('p', 'y') टपल बनाता है।

Q9. What is the output of print(len(('abc',)), len(('abc')))?

(print(len(('abc',)), len(('abc'))) का आउटपुट क्या होगा?)

(A) 3 3

(B) TypeError

(C) 1 1

(D) 1 3

Correct Answer: (D)

[EN] ('abc',) is a 1-element tuple (length 1), whereas ('abc') is simply the string 'abc' (length 3).

[HI] ('abc',) एक 1-तत्व टपल है जिसकी लंबाई 1 है, जबकि ('abc') एक स्ट्रिंग है जिसकी लंबाई 3 है।

Q10. What does t = (1, 2) + (3,); print(t) produce?

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

(A) TypeError

(B) (4, 2)

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

(D) (1, 2, 3)

Correct Answer: (D)

[EN] Concatenating two tuples with + creates a brand-new tuple (1, 2, 3) without mutating the originals.

[HI] + ऑपरेटर दो टपल्स को जोड़कर एक नया टपल (1, 2, 3) बनाता है।

Q11. What happens when you execute (1, 2) + (3)?

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

(A) (4, 2)

(B) (1, 2, 3)

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

(D) Raises TypeError

Correct Answer: (D)

[EN] (3) is an int, not a tuple, so tuple + int fails with TypeError: can only concatenate tuple (not "int") to tuple.

[HI] (3) एक int है (टपल नहीं), इसलिए टपल और int को जोड़ने पर TypeError आता है।

Q12. What is the result of (10,) * 3?

((10,) * 3 का परिणाम क्या होगा?)

(A) 30

(B) TypeError

(C) (10, 10, 10)

(D) (30,)

Correct Answer: (C)

[EN] Repeating a 1-element tuple with * 3 produces a 3-element tuple (10, 10, 10).

[HI] एकल-तत्व टपल (10,) को 3 से गुणा करने पर (10, 10, 10) टपल बनता है।

Q13. Why do tuples typically consume slightly less memory than lists of the same elements in CPython?

(CPython में समान तत्वों वाली लिस्ट की तुलना में टपल सामान्यतः कम मेमोरी क्यों लेते हैं?)

(A) Tuples omit reference counts on elements

(B) Tuples store elements on disk instead of RAM

(C) Tuples allocate exact fixed capacity without growth overallocation buffers

(D) Tuples compress integers into 8-bit fields

Correct Answer: (C)

[EN] Lists over-allocate spare pointer slots for amortized O(1) append(), whereas immutable tuples allocate exact size.

[HI] लिस्ट भविष्य में जुड़ने वाले तत्वों के लिए अतिरिक्त बफर मेमोरी आरक्षित रखती है, जबकि टपल ठीक उतनी ही मेमोरी लेता है।

Q14. What is the output of t = (5, 10, 15, 20); print(t[1:3])?

(t = (5, 10, 15, 20); print(t[1:3]) का आउटपुट क्या होगा?)

(A) TypeError

(B) [10, 15]

(C) (10, 15, 20)

(D) (10, 15)

Correct Answer: (D)

[EN] Slicing a tuple works identically to list slicing and returns a new tuple containing the sliced items.

[HI] टपल की स्लाइसिंग लिस्ट जैसी ही होती है और परिणाम स्वरूप एक नया टपल (10, 15) लौटाती है।

Q15. Which expression creates an empty tuple?

(निम्न में से कौन-सा एक्सप्रेशन एक खाली टपल बनाता है?)

(A) set()

(B) (,)

(C) {}

(D) tuple()

Correct Answer: (D)

[EN] () or tuple() creates an empty tuple; (,) is a SyntaxError, {} creates an empty dict, and set() creates an empty set.

[HI] () या tuple() खाली टपल बनाते हैं; (,) से SyntaxError आता है और {} खाली डिक्शनरी बनाता है।

Q16. Which statement is NOT true about Python tuples?

(पायथन टपल के बारे में कौन-सा कथन सत्य नहीं है?)

(A) Creating a 1-element tuple requires a trailing comma

(B) Every tuple can always be used as a dictionary key regardless of its contents

(C) Tuples support slicing and negative indexing

(D) Tuples preserve insertion order and allow duplicates

Correct Answer: (B)

[EN] A tuple is hashable (and usable as a dict key) ONLY if all its elements are also hashable; a tuple containing a list cannot be a key.

[HI] टपल तभी डिक्शनरी की बन सकता है जब उसके भीतर के सभी तत्व हैशेबल हों; लिस्ट रखने वाला टपल की नहीं बन सकता।

Q17. Within Python Tuples: Creation, Parentheses, Single Element Tuple (x,), which tuple expression directly maps to: "tuple of length 0"?

(Python Tuples: Creation, Parentheses, Single Element Tuple (x,) में कौन सा Tuple Expression सीधे "tuple of length 0" से संबंधित है?)

(A) (5,)

(B) (5)

(C) 1, 2, 3

(D) ()

Correct Answer: (D)

[EN] In Python Tuples: Creation, Parentheses, Single Element Tuple (x,), '()' handles 'tuple of length 0' (Only tuple syntax created without a comma).

[HI] Python Tuples: Creation, Parentheses, Single Element Tuple (x,) में '()' का कार्य/विवरण 'tuple of length 0' (Only tuple syntax created without a comma) है।

Q18. Which option accurately describes '(5)' in Python Tuples: Creation, Parentheses, Single Element Tuple (x,)?

(Python Tuples: Creation, Parentheses, Single Element Tuple (x,) के संदर्भ में '(5)' की सही विशेषता कौन सी है?)

(A) tuple of length 1

(B) tuple (1, 2, 3)

(C) tuple of length 0

(D) int with value 5

Correct Answer: (D)

[EN] '(5)' provides 'int with value 5' — key point: Parentheses alone act as grouping, NOT a tuple.

[HI] '(5)' का मुख्य विवरण 'int with value 5' है (Parentheses alone act as grouping, NOT a tuple)।

Q19. In Python Tuples: Creation, Parentheses, Single Element Tuple (x,), identify the correct parameter for the specification: "tuple of length 1".

(Python Tuples: Creation, Parentheses, Single Element Tuple (x,) में "tuple of length 1" विवरण किस विकल्प के लिए सही है?)

(A) 1, 2, 3

(B) ()

(C) (5)

(D) (5,)

Correct Answer: (D)

[EN] '(5,)' corresponds to 'tuple of length 1' (Trailing comma is mandatory for single-element tuple).

[HI] '(5,)' का संबंध 'tuple of length 1' (Trailing comma is mandatory for single-element tuple) से है।

Q20. Considering Python Tuples: Creation, Parentheses, Single Element Tuple (x,), what key behavior defines '1, 2, 3'?

(Python Tuples: Creation, Parentheses, Single Element Tuple (x,) में '1, 2, 3' की मुख्य भूमिका या मान क्या है?)

(A) tuple (1, 2, 3)

(B) int with value 5

(C) tuple of length 1

(D) tuple of length 0

Correct Answer: (A)

[EN] '1, 2, 3' represents 'tuple (1, 2, 3)' (Commas without parentheses still pack into a tuple).

[HI] '1, 2, 3' का अर्थ/मान 'tuple (1, 2, 3)' है (Commas without parentheses still pack into a tuple)।

Python Tuples: Creation, Parentheses, Single Element Tuple (x,) — Exam FAQs

Q1: Why is a trailing comma needed for (5,) but not for ()?

(5,) में कॉमा लगाना जरूरी क्यों है जबकि खाली टपल () में नहीं?

[EN] Parentheses around a single expression like (5) are already reserved for mathematical grouping such as (2 + 3) * 4. Empty parentheses () have no arithmetic meaning, so Python unambiguously parses () as an empty tuple.

[HI] किसी एक मान के चारों ओर () को गणितीय ग्रुपिंग के लिए आरक्षित रखा गया है, जैसे (2 + 3) * 4। वहीं खाली () का कोई गणितीय अर्थ नहीं है, इसलिए पायथन उसे सीधे खाली टपल मानता है।

Q2: If tuples are immutable, how does t = t + (4,) work?

यदि टपल इम्यूटेबल हैं, तो t = t + (4,) कैसे काम करता है?

[EN] It does NOT modify the original tuple in place. Instead, + builds a brand-new tuple combining the old items and 4, and rebinds the variable name t to that new object (changing id(t)).

[HI] यह मूल टपल को इन-प्लेस नहीं बदलता, बल्कि + एक नया टपल बनाकर वेरिएबल t को उस नए ऑब्जेक्ट से जोड़ देता है (id(t) बदल जाती है)।

Q3: When should I prefer a tuple over a list?

लिस्ट के बजाय टपल को कब प्राथमिकता देनी चाहिए?

[EN] Use a tuple for fixed heterogeneous records (like (x, y) coordinates or RGB colors), returning multiple values from functions, protecting constants from accidental mutation, and creating dictionary keys.

[HI] निश्चित रिकॉर्ड (जैसे (x, y) निर्देशांक), फंक्शन से एक साथ कई मान लौटाने, डेटा को अनचाहे बदलाव से बचाने और डिक्शनरी की (key) बनाने के लिए टपल बेहतर है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)