๐ฏ Python Common Interview Questions and Answers
Top Python interview questions asked at MNCs, and startups โ with clear answers and code examples.
Practice live on Pynfinity's Mock Interview tool โ randomized questions, instant scoring: pynfinity.com/preparation/python
๐ป Code Example:
# Q1: What is the difference between list and tuple? my_list = [1, 2, 3] # Mutable โ can change my_tuple = (1, 2, 3) # Immutable โ cannot change my_list[0] = 99 # Works fine # my_tuple[0] = 99 # TypeError! # Q2: What is a decorator? def pynfinity_logger(func): def wrapper(*args, **kwargs): print(f"Calling: {func.__name__}") result = func(*args, **kwargs) print(f"Done: {func.__name__}") return result return wrapper @pynfinity_logger def greet(name): return f"Hello, {name}!" greet("santoshtvk") # Q3: What is *args and **kwargs? def tvk_function(*args, **kwargs): print("Args:", args) # Tuple of positional args print("Kwargs:", kwargs) # Dict of keyword args tvk_function(1, 2, 3, name="dhruv", platform="pynfinity") # Q4: List comprehension vs map() squares_lc = [x**2 for x in range(5)] # List comp squares_map = list(map(lambda x: x**2, range(5))) # map print(squares_lc == squares_map) # True
| Concept | Key Takeaway |
|---|---|
| list vs tuple | list is mutable; tuple is immutable and faster |
| is vs == | 'is' checks identity (same object); '==' checks equality (same value) |
| deepcopy vs copy | copy() is shallow; deepcopy() copies nested objects too |
| GIL | Global Interpreter Lock โ only one thread executes Python bytecode at a time |
| Generator vs List | Generators are lazy/memory-efficient; lists load all data at once |
Keep exploring and happy coding! ๐