Python Technical Interview Questions and Answers – Set 2 (50 Questions)

Prepare for Python interviews with this set of 50 advanced and technical questions. Includes explanations and code examples.


Advanced Python Concepts

1. What are Python modules?

Modules are files containing Python definitions and functions.

import math
print(math.sqrt(16))

2. Difference between module and package?

Module: single Python file
Package: directory containing multiple modules with __init__.py

3. What are Python namespaces?

x = 5  # global
def func():
    y = 10  # local

4. What is Python’s GIL?

Global Interpreter Lock ensures only one thread executes Python bytecode at a time.

5. What are Python iterators?

lst = [1,2,3]
it = iter(lst)
print(next(it))  # 1

6. What are Python generators?

def gen():
    for i in range(3):
        yield i
for val in gen(): print(val)

7. Difference between iterators and generators?

Generators are iterators but don’t store all items in memory and are defined using yield.

8. What is Python’s zip() function?

a=[1,2]; b=['x','y']
print(list(zip(a,b)))  # [(1,'x'),(2,'y')]

9. What is enumerate()?

lst = ['a','b']
for i,val in enumerate(lst):
    print(i, val)

10. What is Python’s with statement?

Used to handle resources like files automatically.


Data Structures & Collections

11. Difference between list, tuple, set, and dict?

List: mutable, ordered
Tuple: immutable, ordered
Set: unordered, unique
Dict: key-value pairs

12. How to sort a list?

lst = [3,1,2]
lst.sort()

13. How to reverse a list?

lst = [1,2,3]
lst.reverse()

14. Difference between deepcopy and copy?

Deepcopy duplicates nested objects; copy only copies top-level references.

15. How to merge two dictionaries?

a={'x':1}; b={'y':2}
c = {**a, **b}

16. What is a Python deque?

from collections import deque
d = deque([1,2])
d.appendleft(0)

17. Difference between is and ==?

is checks identity; == checks value equality.

18. What is Python’s Counter?

from collections import Counter
c = Counter('aaab')

19. Difference between mutable and immutable objects?

Mutable can change after creation; immutable cannot.

20. What are defaultdict and OrderedDict?

defaultdict: auto-creates default values
OrderedDict: preserves insertion order


Functions & OOP

21. What is method overloading in Python?

Python does not support traditional method overloading; use default arguments instead.

22. What is method overriding?

Child class provides a new implementation of a parent method.

23. Difference between class, instance, and static methods?

Instance: needs self
Class: uses @classmethod, needs cls
Static: uses @staticmethod, no self or cls

24. Example of a class method

class A:
    count=0
    @classmethod
    def show(cls):
        print(cls.count)

25. Example of a static method

class A:
    @staticmethod
    def greet():
        print("Hi")

26. What is multiple inheritance?

A class can inherit from more than one parent class.

27. What are Python properties?

class A:
    def __init__(self,x):
        self._x=x
    @property
    def x(self):
        return self._x

28. Difference between __str__ and __repr__?

__str__: user-friendly; __repr__: developer-friendly.

29. What are Python dunder methods?

Special methods like __init__, __str__, __add__ used for operator overloading and object behavior.

30. How to implement operator overloading?

class A:
    def __init__(self,x):
        self.x=x
    def __add__(self,other):
        return self.x + other.x

Exceptions & File Handling

31. Difference between raise and assert?

raise: manually throws exception
assert: tests a condition, raises AssertionError if False

32. How to create custom exceptions?

class MyError(Exception):
    pass
raise MyError("Custom error")

33. How to read JSON files?

import json
with open("data.json") as f:
    data = json.load(f)

34. How to write JSON files?

with open("out.json","w") as f:
    json.dump(data,f)

35. Difference between read(), readline(), and readlines()?

read(): whole file
readline(): one line
readlines(): list of lines

36. How to append to a file?

with open("file.txt","a") as f:
    f.write("text")

37. What is seek()?

Moves file pointer to a specific position.

38. Difference between text and binary files?

Text: human-readable; Binary: raw bytes.

39. What is the csv module?

Used to read and write CSV files.

40. How to handle large files efficiently?

Use generators or read line by line in a loop.


Libraries & Tools

41. What is itertools?

Module providing efficient looping tools.

42. What is functools?

Provides higher-order functions like reduce and lru_cache.

43. What are map(), filter(), reduce()?

map(): applies function to all items
filter(): filters items
reduce(): reduces using a function

44. What is any() and all()?

any(): True if any element True
all(): True if all elements True

45. What is eval()?

Evaluates a string as Python code.

46. What is exec()?

Executes Python code dynamically from a string.

47. Difference between shallow and deep comparison?

Shallow: compares objects directly
Deep: compares nested content.

48. How to profile Python code?

Use cProfile or timeit module.

49. What is monkey patching?

Modifying classes or modules at runtime.

50. How to debug Python code?

Use pdb module or IDE debugger.


Comments

Popular posts from this blog

How to Install Geany 2.1 on Windows 10/11 (2026) | Step-by-Step Guide

How to Uninstall Bluefish 2.2.19 on Windows 10/11 (2026) | Step-by-Step Guide

How to Install Visual Studio 2026 on Windows 10/11 | Step-by-Step Guide