✅ 50 Python Interview Questions & Answers
✅ 50 Python Interview Questions & Answers 🐍💼
1. What is Python?
Python is a high-level, interpreted, dynamically typed language supporting OOP, procedural, and functional programming.
2. What are Python’s key features?
-
Easy syntax & readability
-
Interpreted & dynamically typed
-
Extensive standard library
-
Cross-platform
-
Large community support
3. Python’s built-in data types?
-
Numeric:
int,float,complex -
Sequence:
str,list,tuple -
Set types:
set,frozenset -
Mapping:
dict -
Boolean & None:
bool,NoneType
4. Difference between list, tuple, and set?
| Type | Ordered | Mutable | Example |
|---|---|---|---|
| List | Yes | Yes | [1, 2, 3] |
| Tuple | Yes | No | (1, 2, 3) |
| Set | No | Yes | {1, 2, 3} |
5. What are *args and **kwargs?
-
*args→ variable positional arguments -
**kwargs→ variable keyword arguments
6. Lambda function?
A one-line anonymous function:
7. List comprehension?
Concise way to create lists:
8. Modules vs Packages?
-
Module: Single
.pyfile -
Package: Folder with multiple modules &
__init__.py
9. Shallow copy vs Deep copy?
-
Shallow → copies references
-
Deep → copies nested objects
10. is vs ==?
-
==→ checks values -
is→ checks object identity
11. What is Python’s GIL?
Global Interpreter Lock ensures only one thread executes Python bytecode at a time, limiting CPU-bound multithreading.
12. Python memory management?
-
Automatic memory management using garbage collection
-
Reference counting keeps track of object usage
13. What are Python decorators?
Functions that modify behavior of another function or class:
14. Python iterators vs iterables?
-
Iterable: Object that can return an iterator (
list,dict) -
Iterator: Object with
__next__()method
15. Generators?
-
Functions using
yieldto produce lazy sequences -
Memory-efficient for large datasets
16. Python with statement?
Manages resources automatically (e.g., file handling):
17. Difference between deepcopy() and copy()?
-
copy()→ shallow copy -
deepcopy()→ copies nested objects recursively
18. Python string methods?
Examples: upper(), lower(), split(), join(), replace(), strip()
19. Python list methods?
Examples: append(), extend(), insert(), remove(), pop(), sort(), reverse()
20. Python dictionary methods?
Examples: keys(), values(), items(), get(), update(), pop()
21. Difference between Python 2 and 3?
-
Python 3 uses Unicode by default
-
print()is a function in Python 3 -
Division
/returns float in Python 3
22. Python set methods?
add(), remove(), union(), intersection(), difference()
23. Difference between mutable and immutable types?
-
Mutable: Can be changed (
list,dict,set) -
Immutable: Cannot be changed (
tuple,str,int)
24. What is slicing in Python?
Extract part of a sequence:
25. Python *args vs **kwargs?
-
*args→ tuple of arguments -
**kwargs→ dict of keyword arguments
26. Python pass, break, continue?
-
pass→ placeholder -
break→ exit loop -
continue→ skip current iteration
27. What is Python enumerate()?
Adds index to iterable elements:
28. Python zip() function?
Combines iterables element-wise:
29. Python map(), filter(), reduce()?
-
map()→ applies function -
filter()→ filters elements -
reduce()→ cumulative computation (fromfunctools)
30. Python any() vs all()?
-
any([False, True])→ True -
all([True, True])→ True
31. Difference between Python == and is?
-
==→ value equality -
is→ object identity
32. What are Python exceptions?
Errors detected during runtime, handled using try-except
33. Custom exceptions?
34. Python super() usage?
Access parent class methods:
35. Python class vs instance variables?
-
Class → shared across instances
-
Instance → unique to object
36. Python @staticmethod vs @classmethod?
-
@staticmethod→ noselforcls -
@classmethod→ receivescls
37. Difference between Python isinstance() and type()?
-
isinstance(obj, cls)→ True for subclasses too -
type(obj)→ exact class
38. Python __str__ vs __repr__?
-
__str__→ readable string -
__repr__→ unambiguous, for dev
39. Python * operator uses?
-
Multiply numbers
-
Unpack iterables in function calls:
40. Python ** operator?
-
Power:
2**3 = 8 -
Unpack dictionaries in function calls
41. Python del statement?
Deletes variables, list elements, or dict keys:
42. Python id() function?
Returns memory address of an object
43. Python globals() vs locals()?
-
globals()→ dictionary of global symbols -
locals()→ dictionary of local symbols
44. Difference between iterable and iterator?
-
Iterable → can produce an iterator
-
Iterator → has
__next__()
45. Python __init__ method?
Constructor to initialize object attributes
46. Python __del__ method?
Destructor called when an object is garbage-collected
47. Python context manager?
Object supporting with statement using __enter__ and __exit__
48. Python dir() function?
Lists attributes and methods of object or module
49. Python help() function?
Shows documentation for modules, classes, functions
50. Difference between Python shallow and deep copies?
-
Shallow → copies references
-
Deep → copies nested objects recursively
Comments
Post a Comment