Your cart is empty

Continue shopping

Your cart

Python Interview Questions

Python Interview Questions - Email Gate

Top 20 Python Interview Questions

1. What is Python? What are its key features?

Python is a high-level, interpreted programming language known for its simplicity and readability. Key features include: - Dynamic typing - Object-oriented programming support - Extensive standard library - Platform independence - Rich ecosystem of third-party packages

2. What is the difference between lists and tuples in Python?

Lists are mutable (can be modified after creation) while tuples are immutable (cannot be modified after creation). Lists use square brackets [] while tuples use parentheses (). Lists generally consume more memory than tuples.

3. How is memory managed in Python?

Python uses automatic memory management with a garbage collector. Objects are automatically deallocated when they are no longer referenced. Python's memory manager handles memory allocation and deallocation behind the scenes.

4. What are decorators in Python?

Decorators are a way to modify or enhance functions or classes without directly changing their source code. They use the @decorator_name syntax and are a form of metaprogramming, allowing you to wrap one function with another function.

5. Explain list comprehension in Python

List comprehension is a concise way to create lists based on existing lists or iterables. It follows the syntax: [expression for item in iterable if condition]. It's often more readable and faster than traditional for loops.

6. What is the Global Interpreter Lock (GIL)?

The GIL is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecode simultaneously. This is specific to CPython and can impact multi-threaded performance in CPU-bound tasks.

7. What are Python generators?

Generators are functions that return an iterator using the yield keyword. They generate values on the fly and are memory efficient as they don't store all values in memory at once, instead generating them as needed.

8. How do you handle exceptions in Python?

Exceptions are handled using try-except blocks. You can catch specific exceptions or use a general except clause. The finally block is used for cleanup code that must run regardless of whether an exception occurred.

9. What is the difference between __init__ and __new__ in Python?

__new__ is a static method that creates and returns a new instance of the class, while __init__ initializes the created instance. __new__ is called before __init__ and is rarely overridden except in metaclasses.

10. Explain Python's inheritance and how to implement it

Python supports single and multiple inheritance. A class can inherit attributes and methods from one or more parent classes. It's implemented using the syntax: class Child(Parent). The super() function is used to call methods from the parent class.

11. What are metaclasses in Python?

Metaclasses are classes whose instances are classes themselves. They define how classes are constructed and behave. The default metaclass is 'type'. Metaclasses allow you to customize class creation, modify class attributes, and implement design patterns like Singleton at the class level.

12. Explain the difference between deep copy and shallow copy

Shallow copy creates a new object but references to nested objects are shared. Deep copy creates a completely independent copy including all nested objects. Use copy.copy() for shallow copy and copy.deepcopy() for deep copy. This is crucial when dealing with mutable nested structures.

13. What are closures in Python and how do they work?

A closure is created when a nested function references variables from its enclosing scope. The nested function "closes over" these variables, maintaining access to them even after the outer function returns. Closures are useful for creating function factories and maintaining state in functional programming.

14. Explain Python's descriptor protocol

Descriptors are objects that define how attribute access is handled through __get__, __set__, and __delete__ methods. They're the mechanism behind properties, methods, static methods, and class methods. Understanding descriptors helps explain how Python's attribute access works under the hood.

15. What is the difference between __str__ and __repr__ methods?

__str__ should return a human-readable string representation of the object (for end users), while __repr__ should return an unambiguous string representation (for developers). __repr__ is used by the debugger and should ideally be valid Python code that could recreate the object.

16. How does Python's method resolution order (MRO) work?

MRO determines the order in which methods are inherited in multiple inheritance scenarios. Python uses the C3 linearization algorithm to ensure a consistent and predictable method resolution order. You can view the MRO using the __mro__ attribute or mro() method.

17. What are context managers and how do you implement them?

Context managers implement the context management protocol using __enter__ and __exit__ methods. They're used with the 'with' statement for resource management. You can create them using classes or the @contextmanager decorator. They ensure proper setup and cleanup of resources.

18. Explain Python's import system and module loading

Python's import system involves finders, loaders, and the module cache (sys.modules). When importing, Python searches sys.path for modules, uses importlib for loading, and caches modules to avoid reloading. Understanding this helps with debugging import issues and creating custom importers.

19. What are slots in Python and when should you use them?

__slots__ restricts the attributes that instances of a class can have, saving memory by preventing the creation of __dict__ for each instance. Use slots when you have many instances of a class and want to optimize memory usage, but be aware of the limitations like no dynamic attribute assignment.

20. Explain Python's asyncio and coroutines

Asyncio is Python's library for asynchronous programming using coroutines. Coroutines are functions defined with 'async def' and called with 'await'. They enable concurrent execution without threading overhead, perfect for I/O-bound tasks. The event loop manages and executes coroutines, handling asynchronous operations efficiently.