Core Python Interview Questions and Answers
1. What are the key features of Python?
- Easy to learn and use
- Interpreted and dynamically typed
- Object-Oriented
- Large standard library
- Open-source and portable
2. What is the difference between Python 2 and Python 3?
- Python 3 uses print() as a function
- Integer division returns float by default
- Unicode support is better in Python 3
3. What are Python's data types?
- int, float, complex, str, list, tuple, dict, set, bool, NoneType
4. What is the difference between a list and a tuple?
- List is mutable: [1, 2]
- Tuple is immutable: (1, 2)
5. What are mutable and immutable types in Python?
- Mutable: list, dict, set
- Immutable: int, float, str, tuple
6. What is PEP 8?
- A style guide for Python code formatting and best practices.
7. What are Python's keywords?
- Predefined reserved words like `if`, `else`, `for`, `def`, etc.
8. What is None, pass, break, continue, and del in Python?
- None: null value
- pass: empty statement
- break/continue: control loop
- del: delete variable
9. What are lists, sets, tuples, and dictionaries?
- list: ordered, mutable
- tuple: ordered, immutable
- set: unordered, unique
- dict: key-value pairs
10. How do you iterate over a dictionary?
for k, v in my_dict.items(): print(k, v)
11. How is a set different from a list?
- set: unordered, unique items
- list: ordered, allows duplicates
12. How do you remove duplicates from a list?
list(set(my_list))
13. What are *args and **kwargs?
- *args: variable positional args
- **kwargs: variable keyword args
14. What is the difference between global, local, and nonlocal scope?
- global: outside function
- local: inside function
- nonlocal: nested function scope
15. What is recursion? Give an example.
A function calling itself:
def fact(n): return 1 if n==0 else n*fact(n-1)
16. What is lambda in Python?
- Anonymous function: lambda x: x+1
17. What is a decorator?
- A function that wraps another function
18. What are higher-order functions?
- Functions that take or return other functions
19. What is OOP in Python?
- Programming with classes and objects, encapsulation, inheritance, etc.
20. What are classes and objects?
- Class: blueprint
- Object: instance of class
21. What is __init__ method?
- Constructor method called on object creation
22. What are self and cls?
- self: instance reference
- cls: class reference
23. Explain inheritance and types of inheritance in Python.
- Single, multiple, multilevel, hierarchical inheritance
24. What is method overriding?
- Subclass method replaces parent method
25. What is polymorphism?
- Same interface, different behavior
26. What are classmethods and staticmethods?
@classmethod uses cls
@staticmethod has no self/cls
27. What is exception handling in Python?
- Handling errors using try-except blocks
28. What is the use of try, except, finally, else?
- try: risky code
- except: error handling
- else: if no error
- finally: always runs
29. What is the difference between raise and assert?
- raise: manual exception
- assert: debug check
30. What are user-defined exceptions?
- Custom exception class: class MyError(Exception): pass
31. How to read and write a file in Python?
with open("file.txt", "r") as f: data = f.read()
32. What are modes in file handling?
- 'r', 'w', 'a', 'rb', 'wb' etc.
33. What is with open() used for?
- Context manager to auto-close files
34. What is a generator?
- Uses yield, returns iterable sequence without storing whole list
35. What is a Python iterator?
- Object with __iter__ and __next__
36. Difference between staticmethod, classmethod, and instance method?
- static: no self
- class: uses cls
- instance: uses self
37. What is list comprehension?
[x*x for x in range(5)]
38. What are modules and packages?
- Module: .py file
- Package: folder with __init__.py
39. Difference between is and ==?
- is: memory
- ==: value comparison
40. What is if __name__ == '__main__'?
- Entry point check for script execution
41. How does Python manage memory?
- Private heap space, garbage collector
42. What is garbage collection?
- Automatic memory cleanup using ref count
43. What are shallow and deep copy?
- shallow: copies references
- deep: copies objects
44. What are Python's built-in functions?
- len(), type(), str(), int(), list(), etc.
45. Difference between array and list in Python?
- array: same type
- list: mixed types allowed
46. Explain map(), filter(), reduce().
- map(func, iterable), filter(condition), reduce(func, seq)
47. What are Python's string functions?
- lower(), upper(), split(), replace(), find(), etc.
48. What is a virtual environment?
- Isolated Python environment: venv or virtualenv
49. What are Python's built-in data structures?
- list, tuple, set, dict
50. Difference between compile-time and run-time errors?
- Compile-time: syntax errors
- Run-time: logic or input errors