Most Asked Python Questions
Basic Python Interview Questions
1. What are Pythons key features?
- Interpreted, high-level, dynamically typed, object-oriented, open-source, portable, supports multiple
paradigms.
2. Difference between list, tuple, set, and dict:
| Type | Ordered | Mutable | Duplicates | Syntax |
|-------|---------|---------|------------|--------|
| list | Yes | Yes | Yes | [] |
| tuple | Yes | No | Yes | () |
| set | No | Yes | No | {} |
| dict | Yes | Yes | Keys: No | {} |
3. Difference between 'is' and '==':
- 'is': checks identity
- '==': checks equality
4. Data types: int, float, str, bool, list, tuple, set, dict, NoneType
5. Memory management:
- Uses reference counting and garbage collection.
Intermediate-Level Questions
1. List comprehension: [x**2 for x in range(5)]
2. Lambda: lambda x: x + 2
3. *args and **kwargs:
def func(*args, **kwargs): pass
4. pass: placeholder
continue: skip to next iteration
break: exit loop
5. copy.copy(): shallow
copy.deepcopy(): deep
OOP in Python
1. Pillars: Inheritance, Encapsulation, Polymorphism, Abstraction
2. Inheritance:
class A: pass
class B(A): pass
3. __init__ is constructor; self is instance
4. Class vs Static methods:
@classmethod def cls_method(cls): pass
@staticmethod def static_method(): pass
5. Method overriding: redefine in child class
Exception Handling
1. try/except/finally
2. Common exceptions: ValueError, TypeError, KeyError, IndexError, ZeroDivisionError
File Handling
1. Reading:
with open('file.txt', 'r') as f: f.read()
2. read(): full file
readline(): one line
readlines(): list of lines
Advanced Topics
1. Decorator example:
def decorator(func):
def wrapper():
print("Before"); func(); print("After")
return wrapper
2. Generator:
def gen(): yield 1; yield 2
3. Iterator vs Iterable:
- Iterable: list, tuple
- Iterator: has __next__()
4. GIL: prevents multiple threads executing Python bytecodes in CPython
5. Multithreading (I/O) vs Multiprocessing (CPU)
Common Coding Questions
1. Reverse string
2. Find duplicates in list
3. Fibonacci
4. Palindrome check
5. Count occurrences
6. Sort list of tuples
7. Merge dictionaries
8. Flatten nested list
9. Find missing number
10. Use of map, filter, reduce