Model answers for 4 frequently asked questions
Speak in 20-40 seconds per answer
Q: What is the difference between a list and a tuple? A: A list is mutable, so you can add or change items; a tuple is immutable. Tuples can be dictionary keys when their items are hashable, and they signal that the data should not change, like (latitude, longitude).
Q: What are *args and **kwargs? A: *args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dictionary. They let a function accept a flexible number of inputs, for example def log(*args, **kwargs).
Q: What is a generator? A: A function that uses yield to produce values one at a time instead of building a full list. It saves memory, so reading a 2GB log file line by line with a generator works where a list would not.
Q: What is the difference between == and is? A: == compares values, while is checks whether two names point to the same object in memory. [1, 2] == [1, 2] is True, but [1, 2] is [1, 2] is False.
Q: How do you handle errors in Python? A: With try and except, catching specific exceptions such as ValueError rather than a bare except. Use else for code that runs when nothing failed and finally for cleanup like closing files.