Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) Notes in Hindi & English : पायथन सेट्स (Sets): अद्वितीय तत्व, अनऑर्डर्ड और कर्ली ब्रेसेस {} और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) (पायथन सेट्स (Sets): अद्वितीय तत्व, अनऑर्डर्ड और कर्ली ब्रेसेस {}) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: A Python set is an unordered, unindexed, mutable collection of unique hashable elements implemented via a hash table. Empty curly braces {} create a dictionary rather than a set, so an empty set must always be instantiated using set().

अध्याय सार: पायथन सेट (set) हैश टेबल पर आधारित अद्वितीय, अनऑर्डर्ड और अनइंडेक्स्ड (असूचीबद्ध) हैशेबल तत्वों का एक म्यूटेबल संग्रह है। खाली कर्ली ब्रेसेस {} सेट के बजाय डिक्शनरी बनाते हैं, इसलिए खाली सेट हमेशा set() द्वारा ही बनाया जाता है।

Python Sets: Characteristics (Unordered, Unindexed, Unique Elements): Detailed Notes & Architecture (पायथन सेट्स (Sets): अद्वितीय तत्व, अनऑर्डर्ड और कर्ली ब्रेसेस {})

1. Unique Elements and Automatic Deduplication (अद्वितीय तत्व और स्वतः डुप्लिकेट हटाना)

[EN] A set strictly prevents duplicate values: supplying repeated items in a set literal like {10, 20, 10, 30} automatically keeps only one copy of each unique element. Equality and hash value determine uniqueness; because 1 == True == 1.0 and hash(1) == hash(True) == hash(1.0), a set {1, True, 1.0} retains only the first inserted representative (1). Converting a list via list(set(lst)) is a classic O(n) deduplication idiom (though it does not preserve order).

[HI] सेट में डुप्लिकेट मान नहीं रह सकते: {10, 20, 10, 30} लिखने पर स्वतः केवल अद्वितीय तत्व ही रखे जाते हैं। समानता (==) और हैश मान अद्वितीयता तय करते हैं; चूंकि 1 == True == 1.0 और उनके हैश बराबर हैं, इसलिए {1, True, 1.0} में केवल पहला मान 1 ही टिकता है। किसी लिस्ट से डुप्लिकेट हटाने के लिए list(set(lst)) एक लोकप्रिय तरीका है।

2. Unordered and Unindexed Characteristics (अनऑर्डर्ड और अनइंडेक्स्ड प्रकृति)

[EN] Unlike lists and tuples, Python sets do NOT maintain positional indices or guarantee insertion order. Trying to index or slice a set like s[0] or s[1:3] immediately raises TypeError: 'set' object is not subscriptable. Furthermore, element iteration order depends on internal hash slots and CPython's randomized hash seed (PYTHONHASHSEED), which can vary across Python interpreter runs for strings.

[HI] लिस्ट और टपल के विपरीत, सेट तत्वों का क्रम या इंडेक्स सुरक्षित नहीं रखते। s[0] या s[1:3] चलाने पर तुरंत TypeError: 'set' object is not subscriptable त्रुटि आती है। सेट में तत्वों का क्रम आंतरिक हैश टेबल स्लॉट्स और PYTHONHASHSEED पर निर्भर करता है, इसलिए स्ट्रिंग्स के लिए यह अलग-अलग रन में बदल सकता है।

3. Curly Braces {} vs set() and Hashability Rules (कर्ली ब्रेसेस {} बनाम set() और हैशेबिलिटी नियम)

[EN] A top exam pitfall is initializing an empty set: {} creates an empty dict (<class 'dict'>), NOT a set, because dictionaries historically claimed {} first in Python. You must call set() for an empty set. Additionally, every element inside a set must be hashable (int, float, str, tuple of immutables, frozenset); inserting mutable objects such as a list, dict, or another set raises TypeError: unhashable type.

[HI] परीक्षा का सबसे प्रसिद्ध प्रश्न खाली सेट बनाना है: {} लिखने से खाली डिक्शनरी (dict) बनती है, खाली सेट नहीं; खाली सेट के लिए set() लिखना अनिवार्य है। साथ ही, सेट के सभी तत्व हैशेबल (इम्यूटेबल जैसे int, str, frozenset या इम्यूटेबल टपल) होने चाहिए; लिस्ट, डिक्शनरी या दूसरे सेट को तत्व बनाने पर TypeError: unhashable type आता है।

4. Hash Table Speed and frozenset (हैश टेबल की गति और frozenset)

[EN] CPython implements set using an open-addressed hash table, giving average-case O(1) time complexity for membership testing (x in s), far outperforming O(n) linear scans on lists or tuples. While a normal set is mutable (and therefore unhashable itself), Python also provides frozenset(iterable)—an immutable, hashable set variant that can serve as a dictionary key or be nested inside another set.

[HI] CPython में सेट ओपन-एड्रेस्ड हैश टेबल का उपयोग करता है, जिससे मेंबरशिप जांच (x in s) की औसत समय जटिलता O(1) होती है—जो लिस्ट की O(n) खोज से अत्यधिक तेज है। सामान्य सेट म्यूटेबल (इसलिए अनहैशेबल) होता है, जबकि इसका इम्यूटेबल रूप frozenset() हैशेबल होता है और इसे डिक्शनरी की (key) या दूसरे सेट के तत्व के रूप में रखा जा सकता है।

Syntax, Formula & Technical Example (Python Sets: Characteristics (Unordered, Unindexed, Unique Elements))

empty_dict = {}
empty_set = set()
s = {10, 20, 10, True, 1}        # {True, 10, 20} or {10, 20, True}
print(type(empty_dict).__name__, type(empty_set).__name__) # dict set
print(len(s), 20 in s)           # 3 True
fs = frozenset([1, 2, 2])
outer = {fs, (3, 4)}             # valid: frozenset & tuple are hashable
print(len(outer))                # 2

Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) Quick Reference & Comparison Table

Set / Collection ExpressionEvaluated Type / BehaviorKey Exam Fact
{}Empty dict (<class 'dict'>)Does NOT create a set; always use set() for empty set
set()Empty set (<class 'set'>)Standard way to instantiate an empty set
{1, True, 1.0}Set of length 1 (equality dedup)1 == True == 1.0 share hash; first entry wins
frozenset([1, 2])Immutable, hashable setCan be placed inside another set or used as dict key

Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) Top 20 Bilingual MCQs with Answers

Q1. What is the data type of x = {} in Python?

(पायथन में x = {} का डेटा टाइप क्या होता है?)

(A) frozenset

(B) tuple

(C) set

(D) dict

Correct Answer: (D)

[EN] Empty curly braces {} create an empty dictionary; set() is required to create an empty set.

[HI] खाली कर्ली ब्रेसेस {} से खाली डिक्शनरी (dict) बनती है; खाली सेट बनाने के लिए set() उपयोग होता है।

Q2. What happens when you execute s = {10, 20, 30}; print(s[0])?

(s = {10, 20, 30}; print(s[0]) चलाने पर क्या होता है?)

(A) Prints None

(B) Raises IndexError

(C) Raises TypeError

(D) Prints 10

Correct Answer: (C)

[EN] Sets are unindexed and unordered, so subscripting s[0] raises TypeError: 'set' object is not subscriptable.

[HI] सेट अनइंडेक्स्ड होते हैं, इसलिए s[0] द्वारा इंडेक्सिंग करने पर TypeError आता है।

Q3. What is the average time complexity of checking x in s for a Python set?

(पायथन सेट में x in s (सदस्यता जांच) की औसत समय जटिलता क्या होती है?)

(A) O(log n)

(B) O(1)

(C) O(n)

(D) O(n log n)

Correct Answer: (B)

[EN] Sets are backed by hash tables, providing constant O(1) average lookup time.

[HI] सेट हैश टेबल पर आधारित होते हैं, जिससे मेंबरशिप जांच औसत O(1) समय में हो जाती है।

Q4. What is the length of s = {1, True, 1.0, 0, False}?

(s = {1, True, 1.0, 0, False} की लंबाई (len) क्या होगी?)

(A) 2

(B) 4

(C) 5

(D) 3

Correct Answer: (A)

[EN] Because 1 == True == 1.0 and 0 == False with matching hashes, only two unique keys (1 and 0) remain.

[HI] 1 == True == 1.0 और 0 == False होने के कारण सेट में केवल दो अद्वितीय मान (1 और 0) बचते हैं।

Q5. Which of the following causes TypeError: unhashable type?

(निम्न में से किससे TypeError: unhashable type त्रुटि आएगी?)

(A) {1, frozenset([2, 3])}

(B) {1, [2, 3]}

(C) {1, (2, 3)}

(D) {1, 'abc'}

Correct Answer: (B)

[EN] Lists are mutable and unhashable, so they cannot be stored inside a set.

[HI] लिस्ट म्यूटेबल और अनहैशेबल होती है, इसलिए इसे सेट के तत्व के रूप में नहीं रखा जा सकता।

Q6. What does set('banana') produce?

(set('banana') चलाने पर कितने तत्वों का सेट बनता है?)

(A) {'b', 'a', 'n', 'a', 'n', 'a'} (length 6)

(B) {'banana'} (length 1)

(C) TypeError

(D) {'b', 'a', 'n'} (length 3)

Correct Answer: (D)

[EN] set() iterates over each character in 'banana' and removes duplicates, leaving 'b', 'a', 'n'.

[HI] set() स्ट्रिंग 'banana' के प्रत्येक अक्षर पर इटरेट करके डुप्लिकेट हटा देता है, जिससे 3 अक्षर ('b', 'a', 'n') बचते हैं।

Q7. Which set variant is immutable and hashable so it can be used as a dictionary key?

(सेट का कौन-सा रूप इम्यूटेबल और हैशेबल होता है जिसे डिक्शनरी की बनाया जा सकता है?)

(A) frozenset

(B) tuple_set

(C) set

(D) constset

Correct Answer: (A)

[EN] frozenset is Python's built-in immutable, hashable set type supporting read-only set operations.

[HI] frozenset पायथन का इम्यूटेबल और हैशेबल सेट टाइप है जिसे डिक्शनरी की या नेस्टेड सेट तत्व बनाया जा सकता है।

Q8. Can a set contain another regular mutable set, e.g., {{1, 2}, 3}?

(क्या एक सेट के भीतर दूसरा सामान्य म्यूटेबल सेट रखा जा सकता है, जैसे {{1, 2}, 3}?)

(A) No, it raises TypeError: unhashable type: 'set'

(B) Yes, nested sets are allowed

(C) Yes, if both are integers

(D) It converts the inner set to a list

Correct Answer: (A)

[EN] Regular sets are mutable and unhashable; only frozenset objects can be nested inside another set.

[HI] नहीं, सामान्य सेट अनहैशेबल होता है इसलिए नेस्टेड सेट पर TypeError आता है; इसके लिए frozenset उपयोग करना होगा।

Q9. What happens if you slice a set using s[1:3]?

(सेट पर s[1:3] स्लाइसिंग करने का प्रयास करने पर क्या होगा?)

(A) Raises ValueError

(B) Raises TypeError

(C) Returns a smaller set

(D) Returns a list

Correct Answer: (B)

[EN] Because sets have no indices, slicing is unsupported and raises TypeError: 'set' object is not subscriptable.

[HI] सेट में इंडेक्स न होने के कारण स्लाइसिंग संभव नहीं है और TypeError आता है।

Q10. What is the output of bool(set())?

(bool(set()) का आउटपुट क्या होगा?)

(A) False

(B) None

(C) TypeError

(D) True

Correct Answer: (A)

[EN] Like empty lists, tuples, and dicts, an empty set is falsy and evaluates to False in boolean context.

[HI] खाली लिस्ट या टपल की तरह, खाली सेट भी बूलियन संदर्भ में False (falsy) माना जाता है।

Q11. Which statement is true about {x*2 for x in [1, 2, 2, 3]}?

({x*2 for x in [1, 2, 2, 3]} के बारे में कौन-सा कथन सत्य है?)

(A) It produces {2, 4, 4, 6}

(B) It is a dictionary comprehension

(C) It is a set comprehension producing {2, 4, 6}

(D) It raises SyntaxError

Correct Answer: (C)

[EN] Curly braces with single expressions (no key: value colon) form a set comprehension and automatically deduplicate values.

[HI] बिना कोलन (:) के कर्ली ब्रेसेस में कॉम्प्रिहेंशन लिखने पर सेट कॉम्प्रिहेंशन बनता है जो {2, 4, 6} देता है।

Q12. What happens if you pass a tuple containing a list into a set: s = {(1, [2]) }?

(यदि सेट में ऐसा टपल डाला जाए जिसमें लिस्ट हो: s = {(1, [2])}, तो क्या होगा?)

(A) Raises ValueError

(B) Raises TypeError: unhashable type: 'list'

(C) Converts [2] into (2,)

(D) Succeeds because the outer object is a tuple

Correct Answer: (B)

[EN] Tuple hashability is recursive: since the inner [2] is unhashable, the whole tuple is unhashable.

[HI] टपल की हैशेबिलिटी अंदर के तत्वों पर भी निर्भर करती है; अंदर लिस्ट होने से पूरा टपल अनहैशेबल होकर TypeError देता है।

Q13. What does len({1, 2, 3, 2, 1}) return?

(len({1, 2, 3, 2, 1}) क्या लौटाता है?)

(A) 3

(B) 4

(C) 5

(D) 2

Correct Answer: (A)

[EN] Duplicate entries 2 and 1 are discarded during set creation, leaving three distinct items.

[HI] सेट बनते समय डुप्लिकेट मान स्वतः हट जाते हैं, अतः तीन अद्वितीय तत्व बचते हैं।

Q14. Which built-in collection preserves neither guaranteed indexing nor duplicate elements?

(निम्न में से कौन-सा बिल्ट-इन संग्रह न तो इंडेक्सिंग रखता है और न ही डुप्लिकेट तत्व?)

(A) tuple

(B) set

(C) str

(D) list

Correct Answer: (B)

[EN] list, tuple, and str are ordered indexed sequences allowing duplicates; set is unordered, unindexed, and unique.

[HI] list, tuple और str क्रमबद्ध सीक्वेंस हैं जो डुप्लिकेट रखते हैं, जबकि set अनऑर्डर्ड, अनइंडेक्स्ड और अद्वितीय है।

Q15. What is the output of {1, 2} == {2, 1}?

({1, 2} == {2, 1} का आउटपुट क्या होगा?)

(A) True

(B) False

(C) None

(D) TypeError

Correct Answer: (A)

[EN] Set equality compares membership regardless of element order; both sets contain 1 and 2.

[HI] सेट समानता क्रम पर निर्भर नहीं करती, बल्कि समान तत्वों की उपस्थिति जांचती है; अतः परिणाम True है।

Q16. Which statement is NOT true about Python sets?

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

(A) Sets are mutable collections of unique elements

(B) Empty curly braces {} create an empty set

(C) Sets do not support indexing or slicing

(D) Membership testing x in s runs in O(1) average time

Correct Answer: (B)

[EN] {} creates an empty dict, not an empty set; set() must be used to instantiate an empty set.

[HI] {} खाली सेट नहीं बल्कि खाली डिक्शनरी (dict) बनाता है; खाली सेट के लिए set() लिखना पड़ता है।

Q17. Within Python Sets: Characteristics (Unordered, Unindexed, Unique Elements), which set / collection expression directly maps to: "Empty dict (<class 'dict'>)"?

(Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) में कौन सा Set / Collection Expression सीधे "Empty dict (<class 'dict'>)" से संबंधित है?)

(A) set()

(B) frozenset([1, 2])

(C) {}

(D) {1, True, 1.0}

Correct Answer: (C)

[EN] In Python Sets: Characteristics (Unordered, Unindexed, Unique Elements), '{}' handles 'Empty dict (<class 'dict'>)' (Does NOT create a set; always use set() for empty set).

[HI] Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) में '{}' का कार्य/विवरण 'Empty dict (<class 'dict'>)' (Does NOT create a set; always use set() for empty set) है।

Q18. Which option accurately describes 'set()' in Python Sets: Characteristics (Unordered, Unindexed, Unique Elements)?

(Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) के संदर्भ में 'set()' की सही विशेषता कौन सी है?)

(A) Empty set (<class 'set'>)

(B) Empty dict (<class 'dict'>)

(C) Immutable, hashable set

(D) Set of length 1 (equality dedup)

Correct Answer: (A)

[EN] 'set()' provides 'Empty set (<class 'set'>)' — key point: Standard way to instantiate an empty set.

[HI] 'set()' का मुख्य विवरण 'Empty set (<class 'set'>)' है (Standard way to instantiate an empty set)।

Q19. In Python Sets: Characteristics (Unordered, Unindexed, Unique Elements), identify the correct parameter for the specification: "Set of length 1 (equality dedup)".

(Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) में "Set of length 1 (equality dedup)" विवरण किस विकल्प के लिए सही है?)

(A) set()

(B) {1, True, 1.0}

(C) frozenset([1, 2])

(D) {}

Correct Answer: (B)

[EN] '{1, True, 1.0}' corresponds to 'Set of length 1 (equality dedup)' (1 == True == 1.0 share hash; first entry wins).

[HI] '{1, True, 1.0}' का संबंध 'Set of length 1 (equality dedup)' (1 == True == 1.0 share hash; first entry wins) से है।

Q20. Considering Python Sets: Characteristics (Unordered, Unindexed, Unique Elements), what key behavior defines 'frozenset([1, 2])'?

(Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) में 'frozenset([1, 2])' की मुख्य भूमिका या मान क्या है?)

(A) Empty set (<class 'set'>)

(B) Set of length 1 (equality dedup)

(C) Empty dict (<class 'dict'>)

(D) Immutable, hashable set

Correct Answer: (D)

[EN] 'frozenset([1, 2])' represents 'Immutable, hashable set' (Can be placed inside another set or used as dict key).

[HI] 'frozenset([1, 2])' का अर्थ/मान 'Immutable, hashable set' है (Can be placed inside another set or used as dict key)।

Python Sets: Characteristics (Unordered, Unindexed, Unique Elements) — Exam FAQs

Q1: Why does {} create an empty dictionary instead of an empty set in Python?

पायथन में {} खाली सेट के बजाय खाली डिक्शनरी क्यों बनाता है?

[EN] Dictionaries existed in Python from its earliest versions using {}, whereas the built-in set literal syntax was added much later in Python 2.7/3.0. Keeping {} as an empty dict preserved backward compatibility.

[HI] पायथन के शुरुआती संस्करणों से ही {} डिक्शनरी के लिए उपयोग होता था, जबकि सेट लिटरल बाद में आया। पुराने कोड को टूटने से बचाने के लिए {} को dict ही रखा गया।

Q2: How can I remove duplicates from a list while preserving the original order?

मूल क्रम को बचाए रखते हुए लिस्ट से डुप्लिकेट तत्व कैसे हटाएं?

[EN] Converting via list(set(lst)) loses order. Instead, in Python 3.7+ where dictionaries preserve insertion order, use list(dict.fromkeys(lst)).

[HI] list(set(lst)) से क्रम बदल जाता है। पायथन 3.7+ में क्रम सुरक्षित रखकर डुप्लिकेट हटाने के लिए list(dict.fromkeys(lst)) उपयोग करें।

Q3: Why does {1, True} keep only one element in a Python set?

पायथन सेट में {1, True} लिखने पर केवल एक ही तत्व क्यों बचता है?

[EN] In Python, bool is a subclass of int where True == 1 and hash(True) == hash(1). Sets treat objects with equal value and equal hash as duplicates.

[HI] पायथन में bool, int का ही सबक्लास है जहाँ True == 1 और दोनों का हैश बराबर होता है। इसलिए सेट इन्हें एक ही मान मानता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)