Python Interview Questions Answers
Python Interview Questions Answers
A: Python is easy to learn, open-source, interpreted, dynamically typed, has extensive libraries,
A: Lists are mutable and ordered. Tuples are immutable and ordered. Sets are mutable but
A: A dictionary stores key-value pairs, while a list stores ordered items. Dictionaries are accessed by
A: Mutable types can be changed (e.g., list, dict, set). Immutable types cannot be changed (e.g., int,
str, tuple).
A: Common data types include int, float, str, bool, list, tuple, set, dict, NoneType, and complex.
A: *args is used to pass variable number of positional arguments, **kwargs is used for keyword
arguments.
A: '=' assigns a value, 'is' checks object identity (same memory location).
A: A namespace is a container where names are mapped to objects. Types: Local, Global, Built-in,
and Enclosing.
A: PEP 8 is Python's style guide. It ensures code readability, consistency, and best practices.
A: Python uses reference counting and a garbage collector to manage memory automatically.
function definition.
A: Lambda is an anonymous function with a single expression. Normal functions are defined using
A: Shallow copy copies references to objects. Deep copy copies the object and all nested objects
recursively.
A: Generators yield items using 'yield' and are more memory-efficient. Iterators require __iter__()
A: [x for x in range(5) if x % 2 == 0] creates [0, 2, 4]. It's a concise way to create lists.
A: @staticmethod doesn't access class or instance. @classmethod takes cls as parameter and
Q: What are Python modules and packages? How do you create them?
A: Modules are .py files. Packages are folders with __init__.py. Create by saving code in files and
directories.
A: Python 3 supports Unicode by default, has better syntax, and is the future; Python 2 is outdated.
A: Metaclasses define behavior of classes. They control class creation using type or custom
metaclass.
A: GIL allows only one thread to execute Python bytecode at a time, affecting multi-threading
performance.
Q: What are Python's data structures and how are they implemented?
A: Common ones: list (dynamic array), dict (hash map), set (hash table), tuple (immutable list).
A: Uses reference counting and cycles are handled by the garbage collector using generational GC.
A: Multithreading runs threads in one process, limited by GIL. Multiprocessing runs separate