📑 Table of Contents — किसी भी topic पर सीधे जाएँ
Sequence Data Types क्या हैं? — Ordered Collections का परिवार
🗂️ Python के मुख्य Collection Data Types
| Type | Syntax | Ordered? | Mutable? | Duplicates? |
|---|---|---|---|---|
| String | "text" | ✔ हाँ | ✘ नहीं | ✔ हाँ |
| List | [ ] | ✔ हाँ | ✔ हाँ | ✔ हाँ |
| Tuple | ( ) | ✔ हाँ | ✘ नहीं | ✔ हाँ |
| Dictionary | {key: value} | ✔ (3.7+) | ✔ हाँ | ✘ keys unique |
| Set | { } | ✘ नहीं | ✔ हाँ | ✘ नहीं |
Indexing व Slicing — एक item या टुकड़ा निकालना
Indexing — Positive व Negative दोनों दिशा से
name = "PYTHON" print(name[0]) # P (pehla) print(name[3]) # H (chautha) print(name[-1]) # N (aakhri) print(name[-3]) # T (peeche se teesra)
H
N
T
Slicing [start:stop:step] — stop हमेशा excluded!
text = "PYTHON" print(text[1:4]) # YTH (index 1,2,3) print(text[:3]) # PYT (shuru se index 2 tak) print(text[2:]) # THON (index 2 se aakhir tak) print(text[::2]) # PTO (har 2nd character) print(text[::-1]) # NOHTYP (poori ulti!)
PYT
THON
PTO
NOHTYP
🔤 String Methods — पूरी List (Exam में बहुत पूछे जाते हैं)
| Method | काम | Example |
|---|---|---|
| upper() | सभी अक्षर CAPITAL | "hi".upper() → "HI" |
| lower() | सभी अक्षर छोटे | "HI".lower() → "hi" |
| title() | हर शब्द का पहला अक्षर Capital | "hello world".title() → "Hello World" |
| capitalize() | सिर्फ पहला अक्षर Capital | "hello".capitalize() → "Hello" |
| strip() | शुरू व अंत की whitespace हटाना | " hi ".strip() → "hi" |
| lstrip() / rstrip() | सिर्फ बाएँ / सिर्फ दाएँ की whitespace | " hi".lstrip() → "hi" |
| split(sep) | String को list में तोड़ना | "a,b,c".split(",") → ['a','b','c'] |
| join(list) | List को string में जोड़ना (उल्टा split) | "-".join(['a','b']) → "a-b" |
| replace(old,new) | Substring बदलना | "cat".replace("c","b") → "bat" |
| find(sub) | पहली मिलान की index (न मिले तो -1) | "hello".find("l") → 2 |
| count(sub) | Substring कितनी बार आया | "hello".count("l") → 2 |
| startswith() / endswith() | शुरुआत/अंत जाँचना (True/False) | "hello".startswith("he") → True |
| isalpha() / isdigit() / isalnum() | सिर्फ letters / सिर्फ digits / दोनों जाँचना | "abc".isalpha() → True |
| len(string) | Length (built-in function, method नहीं) | len("hello") → 5 |
s = "python programming" print(s.upper()) print(s.title()) print(s.capitalize())
Python Programming
Python programming
name = " Aditya "
print("[" + name + "]") # spaces ke saath
print("[" + name.strip() + "]") # saaf ho gaya[Aditya]
sentence = "Python is fun"
words = sentence.split() # space se todna
print(words)
joined = "-".join(words) # "-" se wapas jodna
print(joined)
csv_line = "Ravi,20,Lucknow"
fields = csv_line.split(",")
print(fields)Python-is-fun
['Ravi', '20', 'Lucknow']
msg = "I like Java. Java is good."
print(msg.replace("Java", "Python")) # sab jagah badla
print(msg.find("Java")) # pehli baar kahan mila
print(msg.count("Java")) # kitni baar aaya7
2
filename = "report.pdf"
print(filename.startswith("report")) # True
print(filename.endswith(".pdf")) # True
print("Python3".isalpha()) # False - '3' digit hai
print("12345".isdigit()) # True
print("Python3".isalnum()) # True - letters+digits dono chalte hainTrue
False
True
True
s.upper() लिखने भर से s नहीं बदलता, result को किसी variable में रखना पड़ता है: s = s.upper()।Lists — सबसे Powerful, हमेशा बदल सकते हैं
[ ] में लिखा ordered, mutable, heterogeneous collection। सबसे ज़्यादा उपयोग होने वाला data structure — duplicate values allowed, कोई भी data type mix कर सकते हैं।empty = [] numbers = [1, 2, 3, 4] mixed = [10, "Python", 3.14, True] # kai types ek saath! nested = [[1, 2], [3, 4]] # list ke andar list
✏️ List Modify करना — Add, Update, Remove
nums = [10, 20, 30] nums[1] = 99 # update — index se nums.append(40) # end me add nums.insert(1, 15) # position 1 par insert print(nums) nums.remove(99) # value se hataya nums.pop() # aakhri element hataya print(nums)
[10, 15, 30]
📋 Common List Methods
| Method | काम |
|---|---|
| append(x) | अंत में एक element जोड़ना |
| insert(i, x) | index i पर element डालना |
| remove(x) | value से हटाना (पहला मिलान) |
| pop(i) | index से हटाना (default: last) |
| sort() | ascending order (in-place) |
| reverse() | क्रम उल्टा करना |
| clear() | सब कुछ खाली करना |
| extend(iterable) | एक साथ कई elements जोड़ना (list/tuple से) |
| count(x) | x कितनी बार list में है, गिनना |
| index(x) | x की पहली index पता करना |
| copy() | List की नई (independent) copy बनाना |
a = [1, 2, 3]
b = [1, 2, 3]
a.append([4, 5]) # POORI list ek hi element ban kar judi
b.extend([4, 5]) # dono elements ALAG-ALAG jude
print("append:", a)
print("extend:", b)extend: [1, 2, 3, 4, 5]
nums = [10, 20, 30, 20, 40, 20]
print(nums.count(20)) # 20 kitni baar hai
print(nums.index(30)) # 30 ki pehli index
safe_copy = nums.copy()
safe_copy.append(99)
print("Original:", nums) # sanrakshit rahi!
print("Copy:", safe_copy)2
Original: [10, 20, 30, 20, 40, 20]
Copy: [10, 20, 30, 20, 40, 20, 99]
marks = [78, 92, 65, 88, 55]
marks.sort()
print("Sorted:", marks)
print("Length:", len(marks))
print("Max:", max(marks), "| Min:", min(marks))
print("Average:", sum(marks) / len(marks))Length: 5
Max: 92 | Min: 55
Average: 75.6
📦 List Comprehension — Python का जादू
nums = [1, 2, 3, 4, 5] squares = [n * n for n in nums] # har n ka square evens = [n for n in nums if n % 2 == 0] # sirf even print(squares) print(evens)
[2, 4]
Tuples — एक बार बना, हमेशा के लिए Lock
( ) में लिखा ordered, immutable collection। List जैसा दिखता है पर एक बार बनने के बाद बदल नहीं सकता — इसीलिए list से तेज़ व कम memory लेता है।t1 = (1, 2, 3) t2 = (10, "Python", 3.14) single = (5,) # COMMA zaroori hai! bina comma yah int hai not_tuple = (5) # yah sirf int 5 hai, tuple nahi! print(type(single), type(not_tuple))
(5) tuple नहीं है, सिर्फ number 5 है (bracket को math जैसा लिया)! Single-element tuple के लिए comma ज़रूरी: (5,)🔒 List vs Tuple — पूरी तुलना
| आधार | List | Tuple |
|---|---|---|
| Syntax | [ ] | ( ) |
| Mutability | Mutable | Immutable |
| Speed | धीमी | तेज़ |
| Memory | ज़्यादा | कम |
| Methods | बहुत सारे | सिर्फ count(), index() |
| कब उपयोग करें | Data बदलेगा | Data constant रहेगा |
🔢 Tuple के 2 Methods — count() व index()
marks = (78, 92, 78, 65, 78, 88) print(marks.count(78)) # 78 kitni baar hai print(marks.index(92)) # 92 ki pehli index
1
📦 Tuple Unpacking — एक साथ कई variables
student = ("Aditya", 20, "Lucknow")
name, age, city = student # unpacking!
print(f"{name} rehte hain {city} me, age {age}")Dictionaries — Key से Value ढूँढो
{key: value} pairs की collection। Index number नहीं, key से access होती है — जैसे असली dictionary में शब्द (key) से meaning (value) ढूँढते हैं। Keys unique व immutable होनी चाहिए।Dictionary — Key ➜ Value Mapping
student = {"name": "Amit", "age": 20, "course": "Python"}
print(student["name"]) # direct access
print(student.get("age")) # safe access (error nahi dega)
print(student.get("city", "N/A")) # default value agar key nahi mili20
N/A
✍️ Add, Update, Remove
d = {"name": "Amit", "age": 20}
d["course"] = "Python" # naya key-value add
d["age"] = 21 # existing update
print(d)
d.pop("age") # key hataya
print(d){'name': 'Amit', 'course': 'Python'}
📋 Dictionary के बाकी महत्वपूर्ण Methods
| Method | काम |
|---|---|
| keys() | सभी keys की view देना |
| values() | सभी values की view देना |
| items() | सभी key-value pairs की view देना |
| update(other_dict) | दूसरी dictionary से values merge करना |
| setdefault(key, default) | key हो तो value दो, न हो तो default डालकर दो |
| popitem() | आख़िरी जोड़ा हुआ key-value हटाना |
student = {"name": "Amit", "age": 20, "course": "Python"}
print(list(student.keys()))
print(list(student.values()))['Amit', 20, 'Python']
d1 = {"name": "Amit", "age": 20}
d2 = {"age": 21, "city": "Lucknow"} # age common hai
d1.update(d2)
print(d1)← age overwrite हो गई (d2 वाली value जीती), city नई जुड़ी।
d = {"name": "Amit"}
d.setdefault("age", 18) # "age" nahi thi -> 18 daal di
d.setdefault("name", 99) # "name" pehle se hai -> nahi badli
print(d)
d.popitem() # aakhri jodi hatai
print(d){'name': 'Amit'}
Sets — Unique Items की टोकरी
{ } में unordered, mutable collection जिसमें duplicate values नहीं होतीं। Mathematical set operations (Union, Intersection, Difference) के लिए बना है।nums = [1, 2, 2, 3, 3, 3, 4]
unique = set(nums)
print(unique)
empty = set() # NOTE: {} khaali dictionary hai, set nahi!Set Operations — Venn Diagrams
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print("Union :", A | B) # sab kuch dono se
print("Intersection:", A & B) # sirf common
print("Difference :", A - B) # A me hai, B me nahi
print("Symmetric :", A ^ B) # dono me alag-alag jo haiIntersection: {3, 4}
Difference : {1, 2}
Symmetric : {1, 2, 5, 6}
✏️ Set Modify करने के Methods
| Method | काम |
|---|---|
| add(x) | एक element जोड़ना |
| remove(x) | Element हटाना (न मिले तो KeyError) |
| discard(x) | Element हटाना (न मिले तो कोई error नहीं — safe) |
| clear() | पूरा set खाली करना |
| union() / intersection() / difference() | | , & , − के ही method-रूप (union(), intersection() आदि) |
fruits = {"apple", "mango"}
fruits.add("banana")
print(fruits)
fruits.discard("grape") # grape hai hi nahi — koi error nahi!
print("discard() ke baad bhi safe:", fruits)
# fruits.remove("grape") # yah ERROR degi — KeyError!discard() ke baad bhi safe: {'apple', 'mango', 'banana'}
A = {1, 2, 3}
B = {2, 3, 4}
print(A.union(B)) # same as A | B
print(A.intersection(B)) # same as A & B
print(A.difference(B)) # same as A - B{2, 3}
{1}
Mutability — बदलेगा या नया बनेगा? (Recap + Deep Dive)
List (Mutable) vs Tuple (Immutable) — Memory में क्या होता है
nums = [10, 20, 30]
print("List id before:", id(nums))
nums.append(40)
print("List id after :", id(nums)) # SAME
text = "Python"
print("Str id before:", id(text))
text = text + "3"
print("Str id after :", id(text)) # ALAGList id after : 140234567800 (same!)
Str id before: 140234598200
Str id after : 140234598350 (नई!)
🧩 Interesting: Tuple के अंदर List — मिश्रित behavior
t = (1, [2, 3], 4) t[1].append(99) # tuple ka element BADLA nahi, list ke ANDAR add hua print(t) # t[0] = 100 # yah error dega — tuple ke direct elements immutable hain
🎯 Output-Based Questions — List, Tuple, Dict, Set पर
Q1. Negative Indexing
lst = [10, 20, 30, 40, 50] print(lst[-2]) print(lst[1:-1])
[20, 30, 40]
← -2 पीछे से दूसरा; slicing में -1 आखिरी को exclude करता है।
Q2. List की Aliasing (साझा Reference)
a = [1, 2, 3] b = a b.append(4) print(a)
← b = a से दोनों एक ही object को refer करते हैं — b बदला तो a भी बदल गया!
Q3. Tuple Single Element Trap
t = (10) print(type(t))
← comma नहीं है, इसलिए यह tuple नहीं — साधारण int!
Q4. Dictionary get() vs [ ]
d = {"a": 1, "b": 2}
print(d.get("c"))
print(d.get("c", "not found"))not found
← get() key न मिलने पर None (या दिया गया default) लौटाता है — error नहीं देता (d["c"] error देता)।
Q5. Set की Duplicate-Removal Power
s = {1, 2, 2, 3, 3, 3}
print(len(s))← Set duplicates रखता ही नहीं — {1, 2, 3} बना।
Q6. List Slicing से Copy
a = [1, 2, 3] b = a[:] # slice se copy b.append(4) print(a) print(b)
[1, 2, 3, 4]
← a[:] एक नई (अलग) list बनाती है — इसलिए b बदलने पर a सुरक्षित रहा!
Q7. Nested List Access
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(matrix[1][2]) print(matrix[2][0])
7
← पहला index row चुनता है, दूसरा column — [1][2] यानी row 1 का index 2।
Q8. Dictionary Key Overwrite
d = {"a": 1, "b": 2, "a": 3}
print(d)← Keys unique होनी चाहिए — दोहराई गई key की आखिरी value रहती है, पहली overwrite हो जाती है।
Q9. Set Operations Combo
A = {1, 2, 3}
B = {2, 3, 4}
print(A & B)
print(A - B)
print(A ^ B){1}
{1, 4}
← Intersection = common; Difference = A में है B में नहीं; Symmetric = दोनों में अलग-अलग जो है।
Q10. String Slicing Reverse Trick
s = "PROGRAMMING" print(s[2:8:2])
← index 2,4,6 (2 से 8 तक, step 2 में, 8 exclude) → O, G, A।
Q11. List में in operator
fruits = ["apple", "mango", "banana"]
print("mango" in fruits)
print("grape" not in fruits)True
← membership check — दोनों true क्योंकि mango मौजूद है और grape नहीं।
Q12. Tuple Immutability Error
t = (1, 2, 3) t[0] = 100
← Tuple immutable है — किसी index पर direct assignment नहीं हो सकती।
💻 Practice Programs — List, Dict, Set का असली उपयोग
Program 1 — Maximum, Minimum, Mean निकालना
marks = [67, 89, 45, 92, 78, 55]
print("Maximum:", max(marks))
print("Minimum:", min(marks))
print("Mean:", sum(marks) / len(marks))Minimum: 45
Mean: 71.0
Program 2 — Linear Search (बिना built-in के)
nums = [23, 45, 12, 67, 34, 89]
target = int(input("Kya dhoondhna hai: "))
found = False
for i in range(len(nums)):
if nums[i] == target:
print(f"Mila! Index {i} par")
found = True
break
if not found:
print("Nahi mila")Program 3 — Dictionary से Character Frequency Count
text = "banana"
freq = {}
for ch in text:
freq[ch] = freq.get(ch, 0) + 1
print(freq)Program 4 — Student Marksheet (Dictionary)
marks = {"Hindi": 85, "Maths": 92, "Science": 78, "English": 88}
total = sum(marks.values())
percent = total / len(marks)
print(f"Total = {total}")
print(f"Percentage = {percent}%")
best = max(marks, key=marks.get)
print(f"Best subject: {best}")Percentage = 85.75%
Best subject: Maths
Program 5 — List से Duplicates हटाना (Set से)
nums = [5, 2, 8, 2, 5, 9, 8, 1]
unique = list(set(nums))
print("Original:", nums)
print("Unique :", unique)Unique : [1, 2, 5, 8, 9]
Program 6 — Common Students दोनों Classes में (Set Intersection)
science_club = {"Amit", "Priya", "Rahul", "Neha"}
sports_club = {"Priya", "Neha", "Karan"}
both = science_club & sports_club
print("Dono clubs me:", both)Program 7 — List Rotation (Slicing से)
nums = [1, 2, 3, 4, 5]
k = 2 # kitna rotate karna hai
rotated = nums[k:] + nums[:k]
print("Original:", nums)
print("Rotated :", rotated)Rotated : [3, 4, 5, 1, 2]
Program 8 — Word Count Dictionary (sentence से)
sentence = "python is easy python is powerful python rocks"
words = sentence.split()
count = {}
for w in words:
count[w] = count.get(w, 0) + 1
print(count)Summary — Quick Revision (Exam से पहले पढ़ें)
- Sequence Types: String, List, Tuple, Range — ordered, index-based; Dict (3.7+ ordered), Set (unordered)।
- Indexing: 0 से शुरू; negative index पीछे से (-1 = आखिरी)। Slicing: [start:stop:step], stop हमेशा excluded; [::-1] = reverse।
- List [ ] — mutable, duplicates व mixed types allowed; append(), insert(), remove(), pop(), sort()।
- Tuple ( ) — immutable, list से तेज़; (5,) tuple है पर (5) सिर्फ int; सिर्फ count() व index() methods।
- Dictionary {key:value} — key से access (d["key"] या d.get("key")); keys unique व immutable; keys(), values(), items()।
- Set { } — unique elements, unordered, indexing नहीं; set() खाली set, {} खाली dict; Union|, Intersection&, Difference−, Symmetric^।
- Mutability: id() से जाँच — mutable (list/dict/set) में id() same रहती है modify पर; immutable (str/tuple/int) में नई id बनती है।
- Aliasing warning: b = a से दोनों एक ही object refer करते हैं — copy चाहिए तो a[:] या list(a) उपयोग करें।
- List Comprehension: [expression for item in sequence if condition] — compact तरीका नई list बनाने का।
Model Questions — 50 MCQs + 15 Theory Questions
❓ A. Multiple Choice Questions (50)
| # | प्रश्न |
|---|---|
| 1 | List किस symbol में लिखी जाती है? (a) ( )(b) [ ](c) { }(d) < > ✔ सही उत्तर: (b) [ ]Square brackets। |
| 2 | Tuple किस symbol में लिखा जाता है? (a) [ ](b) ( )(c) { }(d) # # ✔ सही उत्तर: (b) ( )Parentheses। |
| 3 | इनमें mutable कौन है? (a) String(b) Tuple(c) List(d) int ✔ सही उत्तर: (c) ListList modify की जा सकती है। |
| 4 | इनमें immutable कौन है? (a) List(b) Dict(c) Set(d) Tuple ✔ सही उत्तर: (d) Tupleबाकी तीनों mutable हैं। |
| 5 | Python में indexing शुरू होती है — (a) 1 से(b) -1 से(c) 0 से(d) 10 से ✔ सही उत्तर: (c) 0 सेZero-based indexing। |
| 6 | lst[-1] देता है — (a) पहला element(b) आखिरी element(c) Error(d) खाली list ✔ सही उत्तर: (b) आखिरी elementNegative index पीछे से गिनता है। |
| 7 | "PYTHON"[1:4] का output — (a) PYT(b) YTH(c) THO(d) YTHO ✔ सही उत्तर: (b) YTHIndex 1,2,3 (4 exclude)। |
| 8 | Slicing में stop index — (a) शामिल होता है(b) शामिल नहीं होता(c) हमेशा 0(d) हमेशा -1 ✔ सही उत्तर: (b) शामिल नहीं होतायह Python का fundamental rule है। |
| 9 | text[::-1] करता है — (a) Sort(b) Reverse(c) Copy(d) Delete ✔ सही उत्तर: (b) Reverseपूरी sequence उल्टी हो जाती है। |
| 10 | List में end पर element जोड़ने का method — (a) add()(b) insert()(c) append()(d) push() ✔ सही उत्तर: (c) append()insert() specific position पर डालता है। |
| 11 | list.remove(x) हटाता है — (a) index x को(b) value x को(c) पहला element(d) आखिरी element ✔ सही उत्तर: (b) value x कोvalue से पहला मिलान हटता है — pop() index से हटाता है। |
| 12 | [1,2,3] + [4,5] का output — (a) [5,7](b) [1,2,3,4,5](c) Error(d) [1,2,3,[4,5]] ✔ सही उत्तर: (b) [1,2,3,4,5]+ list concatenation करता है। |
| 13 | List comprehension का सही syntax — (a) [x for x in seq](b) (x for x in seq)(c) {x: x in seq}(d) x for x in seq ✔ सही उत्तर: (a) [x for x in seq]Square brackets में लिखा जाता है। |
| 14 | (5,) है — (a) int(b) list(c) tuple(d) dict ✔ सही उत्तर: (c) tupleComma की वजह से — यह single-element tuple है। |
| 15 | (5) है — (a) tuple(b) int(c) list(d) error ✔ सही उत्तर: (b) intComma नहीं है — bracket सिर्फ grouping है। |
| 16 | Tuple के मुख्य methods हैं — (a) append, remove(b) count, index(c) add, discard(d) get, update ✔ सही उत्तर: (b) count, indexImmutable होने से modify करने वाले methods नहीं होते। |
| 17 | Tuple, List से तेज़ क्यों है? (a) कम elements रखता है(b) Immutable होने से Python optimize करता है(c) सिर्फ numbers रखता है(d) कोई कारण नहीं ✔ सही उत्तर: (b) Immutable होने सेFixed memory allocation। |
| 18 | Dictionary किस symbol में लिखी जाती है? (a) [ ](b) ( )(c) { }(d) < > ✔ सही उत्तर: (c) { }Curly braces, key:value pairs के साथ। |
| 19 | Dictionary की value access होती है — (a) Index से(b) Key से(c) Position से(d) Random ✔ सही उत्तर: (b) Key सेd["key"] या d.get("key")। |
| 20 | d.get("x") अगर key न मिले तो देता है — (a) Error(b) 0(c) None(d) खाली string ✔ सही उत्तर: (c) Noned["x"] error देता, get() safe है। |
| 21 | Dictionary keys होनी चाहिए — (a) Mutable(b) Unique व Immutable(c) केवल numbers(d) Duplicate allowed ✔ सही उत्तर: (b) Unique व ImmutableList key नहीं बन सकती (mutable है)। |
| 22 | d = {"a":1,"b":2,"a":3} में d होगा — (a) {"a":1,"b":2,"a":3}(b) {"a":3,"b":2}(c) Error(d) {"a":1,"b":2} ✔ सही उत्तर: (b) {"a":3,"b":2}Duplicate key की आखिरी value रहती है। |
| 23 | Dictionary के key-value pairs एक साथ देने वाला method — (a) keys()(b) values()(c) items()(d) pairs() ✔ सही उत्तर: (c) items()for k,v in d.items() पैटर्न में उपयोग। |
| 24 | Set किस symbol में लिखा जाता है? (a) [ ](b) ( )(c) { }(d) ⟨ ⟩ ✔ सही उत्तर: (c) { }Dictionary जैसा ही symbol, पर सिर्फ values। |
| 25 | खाली set बनाने का सही तरीका — (a) {}(b) set()(c) [](d) () ✔ सही उत्तर: (b) set(){} खाली dictionary बनाता है, set नहीं! |
| 26 | {1,2,2,3,3} का length — (a) 5(b) 3(c) 2(d) Error ✔ सही उत्तर: (b) 3Set duplicates automatically हटा देता है — {1,2,3}। |
| 27 | A | B (sets) करता है — (a) Intersection(b) Union(c) Difference(d) XOR ✔ सही उत्तर: (b) Unionदोनों sets का सारा unique data। |
| 28 | A & B (sets) करता है — (a) Union(b) Intersection(c) Difference(d) Copy ✔ सही उत्तर: (b) Intersectionदोनों में जो common है। |
| 29 | A - B (sets) करता है — (a) Union(b) Intersection(c) Difference (A में है B में नहीं)(d) Symmetric ✔ सही उत्तर: (c) DifferenceA - B = A में है पर B में नहीं। |
| 30 | Set indexing support करता है क्या? (a) हाँ(b) नहीं(c) कभी-कभी(d) सिर्फ negative ✔ सही उत्तर: (b) नहींSet unordered है, इसलिए s[0] जैसा कुछ नहीं चलता। |
| 31 | Mutable object modify करने पर id() — (a) बदल जाती है(b) same रहती है(c) 0 हो जाती है(d) None हो जाती है ✔ सही उत्तर: (b) same रहती हैवही object अंदर से बदलता है — object create नहीं होता नया। |
| 32 | Immutable object modify करने पर id() — (a) same रहती है(b) बदल जाती है(c) negative हो जाती है(d) कोई असर नहीं ✔ सही उत्तर: (b) बदल जाती हैनया object बनता है — पुराना वैसा ही रहता है। |
| 33 | b = a (list); b.append(1) करने पर a — (a) नहीं बदलेगी(b) भी बदल जाएगी(c) Error आएगा(d) खाली हो जाएगी ✔ सही उत्तर: (b) भी बदल जाएगीb = a से दोनों एक ही object को refer करते हैं (aliasing)। |
| 34 | List की असली copy बनाने के लिए — (a) b = a(b) b = a[:](c) b = a + 1(d) b == a ✔ सही उत्तर: (b) b = a[:]Slicing से नया object बनता है — copy() भी चलता है। |
| 35 | t = (1, [2,3], 4); t[1].append(9) करने पर — (a) Error आएगा(b) List के अंदर 9 जुड़ जाएगा(c) कुछ नहीं होगा(d) पूरा tuple बदल जाएगा ✔ सही उत्तर: (b) List के अंदर 9 जुड़ जाएगाTuple के अंदर की mutable list खुद बदल सकती है। |
| 36 | List method जो elements को ascending order में रखता है — (a) order()(b) sort()(c) arrange()(d) rank() ✔ सही उत्तर: (b) sort()in-place sort करता है। |
| 37 | max([12, 45, 3, 67, 21]) का output — (a) 3(b) 67(c) 21(d) 12 ✔ सही उत्तर: (b) 67Largest value। |
| 38 | len({"a":1, "b":2, "c":3}) का output — (a) 6(b) 3(c) 1(d) Error ✔ सही उत्तर: (b) 3Dictionary में keys की गिनती। |
| 39 | [1,2,3] * 2 का output — (a) [2,4,6](b) [1,2,3,1,2,3](c) [1,2,3,2](d) Error ✔ सही उत्तर: (b) [1,2,3,1,2,3]* list की repetition करता है, multiplication नहीं। |
| 40 | Nested list matrix[1][2] का मतलब — (a) row 2, col 1(b) row 1, col 2(c) index 12(d) Error ✔ सही उत्तर: (b) row 1, col 2पहला index row चुनता है, दूसरा column। |
| 41 | tuple to list convert करने के लिए — (a) tuple(x)(b) list(x)(c) set(x)(d) dict(x) ✔ सही उत्तर: (b) list(x)Type-casting function। |
| 42 | List से duplicates हटाने का सबसे तेज़ तरीका — (a) for loop से(b) list(set(x))(c) sort()(d) reverse() ✔ सही उत्तर: (b) list(set(x))Set में duplicates ही नहीं रहते। |
| 43 | Dictionary से key हटाने का method — (a) remove()(b) delete()(c) pop()(d) discard() ✔ सही उत्तर: (c) pop()d.pop("key") — set का discard() अलग है। |
| 44 | Set से element हटाने पर जो error नहीं देता — (a) remove()(b) discard()(c) pop()(d) delete() ✔ सही उत्तर: (b) discard()remove() न मिलने पर KeyError देता है। |
| 45 | Tuple unpacking का उदाहरण — (a) x = (1,2,3)(b) a,b,c = (1,2,3)(c) x[0] = 1(d) x.append(1) ✔ सही उत्तर: (b) a,b,c = (1,2,3)एक साथ कई variables में values assign होती हैं। |
| 46 | "apple" in ["apple","mango"] का output — (a) True(b) False(c) 0(d) Error ✔ सही उत्तर: (a) TrueMembership test — apple list में मौजूद है। |
| 47 | List comprehension [n*n for n in [1,2,3]] का output — (a) [1,2,3](b) [1,4,9](c) [2,4,6](d) Error ✔ सही उत्तर: (b) [1,4,9]हर n का square। |
| 48 | Dictionary Python 3.7+ में — (a) पूरी तरह unordered(b) insertion order maintain करती है(c) sorted रहती है(d) random order ✔ सही उत्तर: (b) insertion order maintain करती है3.7 से पहले guarantee नहीं थी। |
| 49 | list.pop() बिना index के हटाता है — (a) पहला element(b) आखिरी element(c) random element(d) सभी elements ✔ सही उत्तर: (b) आखिरी elementDefault index -1 होता है। |
| 50 | Sequence Data Types में शामिल नहीं है — (a) String(b) List(c) Tuple(d) Set ✔ सही उत्तर: (d) SetSet unordered है, इसलिए सही मायने में sequence नहीं। |
📝 B. 15 Theory Questions (Short Answers)
- Sequence Data Types क्या हैं? उदाहरण दीजिए।Items का ordered collection जहाँ हर item का index होता है — String, List, Tuple, Range इसके उदाहरण हैं।
- Indexing और Slicing में अंतर बताइए।Indexing sequence[i] से एक element access करती है; Slicing sequence[start:stop:step] से एक range/subsequence निकालती है — stop हमेशा exclude होता है।
- List और Tuple में 4 अंतर लिखिए।List [ ] mutable, धीमी, ज़्यादा memory, कई methods; Tuple ( ) immutable, तेज़, कम memory, सिर्फ count()/index() methods।
- (5,) और (5) में क्या फर्क है?(5,) comma के कारण single-element tuple है; (5) सिर्फ integer 5 है — bracket को math grouping की तरह लिया जाता है, tuple नहीं बनता।
- Dictionary की keys के क्या नियम हैं?Keys unique होनी चाहिए (repeat हुई तो आखिरी value रहती है) और immutable होनी चाहिए (string, number, tuple चल सकती है; list नहीं क्योंकि वह mutable है)।
- Dictionary में d["key"] और d.get("key") में अंतर बताइए।d["key"] key न मिलने पर KeyError देता है; d.get("key") None (या दिया गया default) return करता है — यह safe access है।
- Set क्या है? इसकी 3 विशेषताएँ लिखिए।{ } में unique, unordered, mutable collection। विशेषताएँ — duplicates नहीं होतीं, indexing/slicing नहीं होती, mathematical operations (Union, Intersection आदि) के लिए बना है।
- Union, Intersection, Difference व Symmetric Difference के symbols व मतलब लिखिए।| Union (दोनों का सारा data), & Intersection (सिर्फ common), − Difference (A में है B में नहीं), ^ Symmetric Difference (दोनों में जो अलग-अलग है)।
- Mutability क्या है? id() से कैसे जाँचते हैं?Object की content बदलने की क्षमता। id() function से पहले व बाद की memory address compare करते हैं — same रहे तो mutable (list/dict/set), बदल जाए तो immutable (str/tuple/int)।
- Aliasing (b = a) की समस्या समझाइए।b = a करने पर दोनों variables एक ही object को refer करते हैं — इसलिए b को modify करने पर a भी बदल जाता है (mutable objects में)। असली copy के लिए a[:] या a.copy() उपयोग करें।
- List Comprehension क्या है? Syntax लिखिए।एक compact तरीका नई list बनाने का — [expression for item in sequence if condition]। जैसे [n*n for n in range(5)] squares की list बनाता है।
- Nested List क्या है? उदाहरण दीजिए।List के अंदर list — जैसे matrix = [[1,2],[3,4]]। matrix[0][1] से पहली row का दूसरा element (2) मिलता है — 2D data (जैसे matrices) के लिए उपयोगी।
- Tuple Unpacking क्या है?Tuple की values को एक साथ कई variables में assign करना — student = ("Amit", 20); name, age = student। Elements की संख्या variables की संख्या से मेल खानी चाहिए।
- List से duplicates हटाने के 2 तरीके बताइए।1) Set का उपयोग: list(set(original_list)) — सबसे तेज़ पर order बदल सकता है। 2) Loop से: नई खाली list बनाकर हर item को "if not in" check करके add करना — order बना रहता है।
- Dictionary से character/word frequency कैसे count करते हैं?freq = {} बनाकर हर item पर freq[item] = freq.get(item, 0) + 1 चलाते हैं — get() से अगर key पहले से न हो तो 0 मिलता है, फिर 1 जुड़ जाता है। हर बार मिलने पर count बढ़ता जाता है।