Python Interview Questions and Answers
0 223
Python Interview Questions & Answers
Preparing for Python interviews? This comprehensive guide features frequently asked questions, in-depth explanations, and practical code examples to help you build confidence and clarity. All code text is explicitly styled in black for readability.
Core Python Fundamentals
- What are Python's core data types?
Examples includeint
,float
,str
,list
,tuple
,dict
,set
, andbool
. - What is list slicing?
You can slice sequences:s[1:5:2]
picks elements from index 1 to 4 in steps of 2. - Explain mutability vs immutability.
Mutable types (like lists, dicts) can change; immutable types (like strings, tuples) cannot.
Control Flow & Functions
- Explain
*args
and**kwargs
.
Use them for variable numbers of positional or keyword arguments. - What are lambda functions?
Anonymous single-expression functions defined withlambda
. - Generators vs list comprehensions?
Generators useyield
, produce lazy sequences, and save memory.
Object-Oriented Programming
- What is method overloading and overriding in Python?
Python doesn’t natively support overloading; last definition wins. Overriding occurs when a subclass defines a method with the same name. - Describe
@classmethod
vs@staticmethod
.
@classmethod
takes the class as its first argument;@staticmethod
takes none. - Explain
__str__
and__repr__
.
__str__
provides readable output;__repr__
gives an unambiguous representation for debugging.
Modules & Libraries
- How to manage dependencies in Python?
Usepip
andrequirements.txt
or tools likepoetry
/pipenv
. - Explain
__init__.py
in packages.
It marks a directory as a Python package and can run initialization code. - Popular built-in modules to know?
Modules likeitertools
,collections
,functools
,os
, andsys
.
Concurrency & Performance
- What is the GIL?
The Global Interpreter Lock ensures only one thread executes Python bytecode at a time. - Multithreading vs multiprocessing?
Threads share memory; processes don’t. Use threads for I/O-bound tasks and processes for CPU-bound tasks.
Code-Based Interview Questions
-
Reverse a string without slicing.
def reverse_str(s): res = '' for ch in s: res = ch + res return res print(reverse_str("coding")) # Output: gnicod
-
Merge two sorted lists.
def merge_sorted(a, b): i = j = 0 res = [] while i < len(a) and j < len(b): if a[i] < b[j]: res.append(a[i]); i += 1 else: res.append(b[j]); j += 1 res.extend(a[i:]); res.extend(b[j:]) return res print(merge_sorted([1,3,5], [2,4,6])) # Output: [1, 2, 3, 4, 5, 6]
-
Check balanced parentheses.
def is_balanced(expr): stack = [] pairs = {'(': ')', '[': ']', '{': '}'} for ch in expr: if ch in pairs: stack.append(ch) elif ch in pairs.values(): if not stack or pairs[stack.pop()] != ch: return False return not stack print(is_balanced("({[]})")) # Output: True print(is_balanced("([)]")) # Output: False
-
Find the most frequent element.
from collections import Counter def most_freq(lst): cnt = Counter(lst) return cnt.most_common(1)[0][0] print(most_freq([1,2,2,3,3,3,2])) # Output: 3
-
Flatten a nested list.
def flatten(lst): flat = [] for el in lst: if isinstance(el, list): flat.extend(flatten(el)) else: flat.append(el) return flat print(flatten([1, [2, [3, 4]], 5])) # Output: [1, 2, 3, 4, 5]
Conclusion
This guide provides in-depth coverage of Python interview topics—core language features, OOP, modules, concurrency, and critical coding challenges. Practicing these examples with black-styled code will ensure clarity and focus while preparing you thoroughly for your next technical interview. Good luck!
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments