1. What are Python’s key features?
Answer:
- Easy to learn and read
- Interpreted language
- Dynamically typed
- Object-oriented
- Large standard library
2. What is the difference between deepcopy() and copy()?
Example:
import copy a = [[1, 2], [3, 4]] b = copy.copy(a) c = copy.deepcopy(a) a[0][0] = 99 print(b) # [[99, 2], [3, 4]] -> shallow copy affected print(c) # [[1, 2], [3, 4]] -> deep copy unaffected
3. What are Python data types?
Answer:
int,float,str,bool,list,tuple,set,dict,NoneType.
4. What is the difference between list and tuple?
Example:
my_list = [1, 2, 3] # Mutable my_tuple = (1, 2, 3) # Immutable
5. What is Python’s __init__ method?
Answer:
It initializes class attributes when an object is created.
class Person:
def __init__(self, name):
self.name = name
p = Person("John")
print(p.name)
⚙️ Intermediate Python
6. Explain *args and **kwargs.
Example:
def test(*args, **kwargs):
print(args)
print(kwargs)
test(1, 2, 3, name="Alice", age=25)
7. What is list comprehension?
Example:
squares = [x**2 for x in range(5)] print(squares) # [0, 1, 4, 9, 16]
8. What is the difference between is and ==?
Example:
a = [1, 2] b = [1, 2] print(a == b) # True (value) print(a is b) # False (different memory)
9. What is lambda in Python?
Example:
square = lambda x: x * x print(square(5)) # 25
10. How do you handle exceptions?
Example:
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Done")
Data Structures
11. How do you sort a dictionary by value?
Example:
d = {'a': 3, 'b': 1, 'c': 2}
sorted_d = dict(sorted(d.items(), key=lambda item: item[1]))
print(sorted_d)
12. Difference between append() and extend()?
Example:
a = [1, 2] a.append([3, 4]) # [1, 2, [3, 4]] a = [1, 2] a.extend([3, 4]) # [1, 2, 3, 4]
13. How to remove duplicates from a list?
Example:
nums = [1, 2, 2, 3, 3] unique = list(set(nums)) print(unique)
14. How do you merge two dictionaries?
Example:
a = {'x': 1, 'y': 2}
b = {'y': 3, 'z': 4}
merged = {**a, **b}
print(merged)
15. What is a generator?
Example:
def gen():
for i in range(3):
yield i
for val in gen():
print(val)
Object-Oriented Programming
16. What is inheritance in Python?
Example:
class Parent:
def greet(self):
print("Hello from parent")
class Child(Parent):
pass
c = Child()
c.greet()
17. What are class methods and static methods?
Example:
class Demo:
@classmethod
def class_m(cls):
print("Class method")
@staticmethod
def static_m():
print("Static method")
Demo.class_m()
Demo.static_m()
18. What is method overriding?
Example:
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
B().show()
Advanced Topics
19. Explain Python decorators.
Example:
def decorator(func):
def wrapper():
print("Before function")
func()
print("After function")
return wrapper
@decorator
def hello():
print("Hello")
hello()
20. What is a context manager?
Example:
with open("file.txt", "w") as f:
f.write("Hello")
# No need to close manually
21. What is __str__ vs __repr__?
Example:
class Test:
def __str__(self):
return "User friendly"
def __repr__(self):
return "Debug info"
print(str(Test()))
print(repr(Test()))
22. What is monkey patching?
Example:
class Test:
def greet(self):
print("Hello")
def new_greet():
print("Hi there")
Test.greet = new_greet
Test().greet()
23. What are Python modules and packages?
Answer:
-
Module: A Python file containing code.
-
Package: A collection of modules (a folder with
__init__.py).
24. Explain Global Interpreter Lock (GIL).
Answer:
GIL ensures that only one thread executes Python bytecode at a time — limiting true parallelism in multi-threaded programs.
25. What are Python iterators?
Example:
nums = [1, 2, 3] it = iter(nums) print(next(it))
26. Explain difference between threading and multiprocessing.
-
Threading: Runs multiple threads (lightweight, shares memory).
-
Multiprocessing: Runs multiple processes (true parallelism).
27. What is the use of with statement?
It simplifies resource management — automatically closes or releases resources.
28. How do you manage virtual environments?
Example Commands:
python -m venv myenv source myenv/bin/activate # Linux/Mac myenv\Scripts\activate # Windows
29. What is the difference between @staticmethod, @classmethod, and normal methods?
| Type | Access to self |
Access to cls |
Purpose |
|---|---|---|---|
| Instance method | ✅ | ❌ | Object-specific |
| Class method | ❌ | ✅ | Class-level |
| Static method | ❌ | ❌ | Utility function |
30. Explain memory management in Python.
-
Python uses automatic garbage collection via reference counting and
gcmodule. -
The memory is managed by the Python private heap.