Python इंटरव्यू प्रश्न और उत्तर
33 प्रश्न, छोटे और साफ़ उत्तरों के साथ · अपडेट: दिसंबर 2026
Python के बारे में developer, data, automation, DevOps और AI रोल के इंटरव्यू में पूछा जाता है। ये प्रश्न उस बुनियादी भाषा से शुरू होते हैं जिस पर हर फ्रेशर को परखा जाता है, फिर उन कॉन्सेप्ट तक जाते हैं जो अनुभवी उम्मीदवारों को समझाने आने चाहिए, जैसे decorators, generators, GIL और memory, और आख़िर में छोटे कोडिंग प्रश्न हैं।
कोडिंग प्रश्नों में इंटरव्यू लेने वाले आपकी सोच को उतना ही देखते हैं जितना उत्तर को: पहले अपना तरीका बोलकर बताइए, फिर साफ़ कोड लिखिए और किसी edge case से टेस्ट कीजिए।
प्रश्न अंग्रेज़ी में रखे गए हैं, क्योंकि इंटरव्यू में ज़्यादातर इसी तरह पूछे जाते हैं। उत्तर हिंदी में हैं, तकनीकी शब्द अंग्रेज़ी में। अंग्रेज़ी उत्तर यहाँ देखें।
Python की बेसिक बातें और data types
Q1. What are the key features of Python?
Python एक high-level, interpreted (bytecode में compile होकर virtual machine पर चलने वाली), dynamically typed, garbage-collected भाषा है जिसका syntax सरल और पढ़ने में आसान है। यह procedural, object-oriented और functional तीनों तरीके सपोर्ट करती है, इसकी standard library बड़ी है और packages का बहुत बड़ा ecosystem है, इसीलिए यह web development, automation, data analysis, machine learning और DevOps scripting में इस्तेमाल होती है।
Q2. What is the difference between mutable and immutable types?
Mutable objects को वहीं बदला जा सकता है: list, dict, set और ज़्यादातर class instances। Immutable objects नहीं बदलते: int, float, str, tuple, frozenset और bytes; इन्हें "बदलने" पर नया object बनता है। सिर्फ़ immutable (hashable) objects ही dictionary की key या set के सदस्य बन सकते हैं, और mutable default arguments से एक मशहूर bug होता है।
Q3. What is the difference between a list and a tuple?
List mutable है, square brackets में लिखी जाती है, और बदलने वाले collections के लिए है। Tuple immutable है, parentheses में लिखा जाता है, थोड़ा तेज़ और छोटा है, और अगर उसके items hashable हों तो खुद भी hashable है, इसलिए dictionary की key बन सकता है; यह coordinates या function के return values जैसे तय records के लिए इस्तेमाल होता है।
Q4. What is the difference between is and ==?
== वैल्यू की तुलना करता है (यह __eq__ बुलाता है); is जाँचता है कि दोनों नाम memory में एक ही object की ओर हैं या नहीं। is सिर्फ़ None जैसे singletons के लिए इस्तेमाल कीजिए (if x is None)। छोटे integers और कुछ strings cache हो सकते हैं, इसलिए उन पर is काम करता दिख सकता है, पर उस पर भरोसा मत कीजिए।
Q5. How are dictionaries implemented, and are they ordered?
Dict एक hash table है: keys को hash करके उनकी जगह ढूँढी जाती है, इसलिए lookup, insert और delete औसतन O(1) होते हैं। Python 3.7 से dictionaries भाषा के नियम के रूप में insertion order बनाए रखती हैं। Keys hashable होनी चाहिए; values कुछ भी हो सकती हैं।
Q6. What are list, dict and set comprehensions?
किसी iterable से, चाहें तो शर्त के साथ, collection बनाने का छोटा तरीका: [x * x for x in nums if x > 0], {name: len(name) for name in names}, {w.lower() for w in words}। ये आम तौर पर append वाले loop से तेज़ और पढ़ने में आसान होते हैं। Generator expression parentheses में लिखा जाता है और items ज़रूरत पड़ने पर एक-एक करके बनाता है।
Q7. What is the difference between a shallow copy and a deep copy?
Shallow copy (list.copy(), copy.copy(), slicing) नया बाहरी object बनाती है पर अंदर के objects शेयर करती है, इसलिए nested list बदलने पर दोनों पर असर पड़ता है। Deep copy (copy.deepcopy) सब कुछ अंदर तक कॉपी करती है, इसलिए दोनों कॉपियाँ पूरी तरह अलग रहती हैं।
Functions
Q8. What are *args and **kwargs?
*args अतिरिक्त positional arguments को एक tuple में और **kwargs अतिरिक्त keyword arguments को एक dictionary में इकट्ठा करता है, ताकि function कितने भी arguments ले सके। Call करते समय यही निशान unpack करते हैं: f(*my_list, **my_dict)। ये decorators और wrapper functions में आम हैं जो arguments आगे भेजते हैं।
Q9. Why is a mutable default argument a problem?
Default वैल्यू सिर्फ़ एक बार, function define होते समय, बनती है, इसलिए def add(item, items=[]) सारी calls में वही list शेयर करता है और वह बढ़ती रहती है। Default None रखिए और list अंदर बनाइए: if items is None: items = []।
Q10. How does Python pass arguments to functions?
Object reference से ("pass by assignment"): parameter का नाम उसी object से जुड़ता है जो caller ने दिया। अगर object mutable है और function उसे वहीं बदलता है, तो caller को बदलाव दिखता है; parameter को किसी नए object से जोड़ने पर caller पर असर नहीं पड़ता।
Q11. What is a lambda function?
एक expression में लिखा छोटा, बिना नाम का function: lambda x: x * 2। यह ज़्यादातर sorted(items, key=lambda p: p["price"]), map या filter जैसे functions को छोटे argument के रूप में दिया जाता है। लंबे काम के लिए साधारण def function ज़्यादा साफ़ है।
Q12. Explain the LEGB scope rule.
Python किसी नाम को इस क्रम में ढूँढता है: Local (मौजूदा function के अंदर), Enclosing (nested functions के लिए बाहरी functions), Global (module स्तर) और Built-in। Function के अंदर global नाम को वैल्यू देनी हो तो global keyword चाहिए, और बाहरी function के variable को वैल्यू देनी हो तो nonlocal।
Q13. What are type hints, and does Python enforce them?
Type hints (def total(prices: list[float]) -> float) अपेक्षित types बताते हैं और mypy, Pyright और editors जैसे टूल को कोड चलाने से पहले गलतियाँ पकड़ने देते हैं। Python खुद run time पर इन्हें लागू नहीं करता; Pydantic और FastAPI जैसी लाइब्रेरी इन्हें validation के लिए इस्तेमाल करती हैं।
Object-oriented programming
Q14. What is self, and what does __init__ do?
self वह instance है जिस पर method बुलाया गया; Python इसे अपने-आप पहले argument के रूप में देता है, इसलिए obj.method() बनता है Class.method(obj)। __init__ नए instance के attributes initialise करता है, object बनने के बाद (object खुद __new__ से बनता है)।
Q15. What is the difference between an instance method, a class method and a static method?
Instance method, self लेता है और एक object के डेटा पर काम करता है। @classmethod, cls लेता है और class पर काम करता है, अक्सर दूसरे constructor के रूप में (Date.from_string(...))। @staticmethod दोनों में से कुछ नहीं लेता; यह साधारण function है जो तार्किक रूप से class से जुड़ा होने के कारण उसमें रखा गया है।
Q16. How does inheritance work, and what is the MRO?
एक class एक या ज़्यादा parent classes से inherit कर सकती है और उनके methods override कर सकती है; super() अगला implementation बुलाता है। Multiple inheritance में Python, Method Resolution Order (C3 linearisation) से तय करता है कि किस class का method इस्तेमाल होगा; ClassName.__mro__ इसे दिखाता है।
Q17. What are dunder (magic) methods?
Double underscore वाले methods जिन्हें Python built-in व्यवहार के लिए बुलाता है: print के लिए __str__ और __repr__, len() के लिए __len__, तुलना के लिए __eq__ और __lt__, + के लिए __add__, indexing के लिए __getitem__, loops के लिए __iter__ और with block के लिए __enter__/__exit__। इन्हें लिखने से आपके objects built-in types जैसा व्यवहार करते हैं।
Q18. What are dataclasses?
@dataclass decorator, annotated class attributes से __init__, __repr__ और __eq__ अपने-आप बना देता है, जिससे मुख्य रूप से डेटा रखने वाली classes में बार-बार का कोड नहीं लिखना पड़ता। frozen=True (immutable), order=True और slots=True जैसे options और व्यवहार जोड़ते हैं।
Generators, decorators और concurrency
Q19. What is the difference between an iterator and a generator?
Iterator कोई भी object है जिसमें __iter__ और __next__ हों, जो एक-एक करके items लौटाए और आख़िर में StopIteration दे। Generator इसे बनाने का आसान तरीका है: yield वाला function, जो रुकता और फिर चलता है, और वैल्यू ज़रूरत पर बनाता है। Generators कम memory लेते हैं, इसलिए बड़ी फ़ाइलों या अनंत sequences के लिए ठीक हैं।
Q20. What is a decorator?
Decorator ऐसा function है जो किसी function को लेकर एक नया function लौटाता है जो उसके आसपास व्यवहार जोड़ता है, और definition के ऊपर @name से लगाया जाता है। आम उपयोग: logging, समय मापना, caching (functools.lru_cache), access जाँच और retries। मूल function का नाम और docstring बचाए रखने के लिए इसके अंदर functools.wraps लगाइए।
Q21. What is a context manager?
with statement के साथ इस्तेमाल होने वाला object जो कुछ सेट करता है और सफ़ाई की गारंटी देता है, चाहे exception आ जाए: with open("f.txt") as f फ़ाइल अपने-आप बंद करता है। इसे __enter__ और __exit__ से, या @contextmanager decorator और एक yield से लिखा जाता है। Locks, database transactions और अस्थायी directories आम उदाहरण हैं।
Q22. What is the GIL, and how does it affect multithreading?
CPython का Global Interpreter Lock एक समय में सिर्फ़ एक thread को Python bytecode चलाने देता है, इसलिए threads ज़्यादा CPU वाले काम को तेज़ नहीं करते, पर I/O वाले काम में फिर भी मदद करते हैं क्योंकि इंतज़ार के समय lock छूट जाता है। ज़्यादा CPU वाले काम के लिए multiprocessing या ऐसी लाइब्रेरी जो GIL छोड़ती हैं, जैसे NumPy, इस्तेमाल कीजिए। Python 3.13 में बिना GIL का optional free-threaded build आया, और 3.14 में यह आधिकारिक रूप से सपोर्टेड हुआ, पर standard build में अब भी GIL है।
Q23. When would you use threading, multiprocessing or asyncio?
एक thread में async और await से बहुत सारे साथ-साथ I/O काम (हज़ारों network calls) के लिए asyncio; ऐसी लाइब्रेरी के साथ I/O काम के लिए threading जो async नहीं हैं; और CPU वाले काम के लिए multiprocessing (या concurrent.futures.ProcessPoolExecutor), क्योंकि अलग processes का अपना interpreter और GIL होता है।
Q24. How does Python manage memory?
CPython, reference counting इस्तेमाल करता है: हर object गिनता है कि कितने references उसकी ओर हैं और गिनती शून्य होने पर वह हटा दिया जाता है। एक cyclic garbage collector (gc module) उन objects को साफ़ करता है जो एक-दूसरे को चक्र में refer करते हैं। छोटे objects की memory Python के अपने allocator से आती है, और tracemalloc जैसे टूल leaks ढूँढने में मदद करते हैं।
Errors, modules और environments
Q25. How does exception handling work in Python?
जो कोड फेल हो सकता है वह try में जाता है; except खास exception types पकड़ता है (सबसे सीमित type पकड़िए, खाली except नहीं); else तब चलता है जब कोई exception न हो; finally हमेशा चलता है, सफ़ाई के लिए। raise exception फेंकता है, और raise ... from e मूल कारण बचाए रखता है। अपनी exceptions, Exception से inherit करने वाली classes होती हैं।
Q26. What is if __name__ == "__main__" for?
जब कोई फ़ाइल सीधे चलाई जाती है तो उसका __name__ "__main__" होता है; import होने पर __name__ module का नाम होता है। शुरू होने वाला कोड इस जाँच के अंदर रखने से फ़ाइल script और import होने लायक module दोनों की तरह काम करती है, बिना import पर वह कोड चलाए।
Q27. Why use a virtual environment, and how do you manage dependencies?
Virtual environment (python -m venv .venv) हर प्रोजेक्ट को अपना interpreter और packages देता है, ताकि अलग versions माँगने वाले प्रोजेक्ट आपस में न टकराएँ और system Python साफ़ रहे। Dependencies pyproject.toml या requirements.txt में लिखी जाती हैं और lock फ़ाइल से pin होती हैं; pip, uv और Poetry जैसे टूल उन्हें हर बार एक जैसा इंस्टॉल करते हैं।
Q28. How do you read a large file without running out of memory?
f.read() या f.readlines() के बजाय file object पर line दर line loop चलाइए (for line in f), जो एक बार में एक line पढ़ता है। Binary फ़ाइलों के लिए loop में तय साइज़ के टुकड़े पढ़िए, और बड़ी CSV के लिए chunksize के साथ pandas या streaming parser इस्तेमाल कीजिए। Generators पूरी pipeline को ज़रूरत पर चलने वाला बनाए रखते हैं।
Q29. How do you test Python code?
pytest (या built-in unittest) से: test_* नाम के छोटे functions लिखिए जो assert इस्तेमाल करें, setup के लिए fixtures, कई inputs के लिए parametrize, और network या database calls बदलने के लिए unittest.mock इस्तेमाल कीजिए। हर बदलाव पर CI में tests चलाइए और coverage.py से coverage मापिए।
कोडिंग प्रश्न
Q30. Reverse a string and check whether it is a palindrome.
Slicing से string उलटती है: s[::-1]। Case और spaces अनदेखा करने वाली palindrome जाँच: clean = "".join(ch.lower() for ch in s if ch.isalnum()); return clean == clean[::-1]। बताइए कि यह समय और जगह दोनों में O(n) है, और two-pointer loop अतिरिक्त कॉपी से बचाता है।
Q31. Count how often each word appears in a text.
from collections import Counter; counts = Counter(text.lower().split()); counts.most_common(5) सबसे ज़्यादा आने वाले पाँच शब्द देता है। Counter के बिना शब्दों पर loop चलाकर counts[w] = counts.get(w, 0) + 1 लिखिए। असली text में पहले punctuation हटाइए, जैसे re.findall(r"\w+", text.lower()) से।
Q32. Find two numbers in a list that add up to a target.
देखी गई वैल्यू की dictionary इस्तेमाल कीजिए: for i, n in enumerate(nums): if target - n in seen: return seen[target - n], i; seen[n] = i। यह nested loop के O(n²) के बजाय O(n) समय लेता है, और यह मशहूर प्रश्न दिखाता है कि आप जानते हैं कि hash lookup memory देकर speed कैसे बढ़ाता है।
Q33. Remove duplicates from a list while keeping the order.
list(dict.fromkeys(items)) हर item की पहली बार वाली जगह क्रम में रखता है, क्योंकि dictionaries insertion order बचाए रखती हैं। list(set(items)) भी duplicates हटाता है पर क्रम खो देता है। दोनों के लिए items hashable होने चाहिए।
Python प्रैक्टिकल तरीके से सीखें
उत्तर याद होना आधा काम है; इंटरव्यू में यह भी पूछा जाता है कि आपने खुद क्या बनाया है। Technical Guftgu की Python ट्रेनिंग हिंदी में लाइव क्लास, लैब और प्रोजेक्ट के साथ होती है, और रिज़्यूमे व मॉक इंटरव्यू में मदद मिलती है। हम नौकरी या प्लेसमेंट की गारंटी नहीं देते।