Core Python Interview Questions Guide
Core Python Interview Questions Guide
Python 2 and Python 3 have several key differences: print statements in Python 2 became print() functions in Python 3, division of integers defaults to floating point in Python 3, Unicode support is stronger in Python 3, and variable scoping rules differ .
Dataclasses in Python 3.7+ provide a decorator and functions for automatically adding special methods such as __init__(), __repr__(), and __eq__() to user-defined classes. They significantly reduce boilerplate code needed to create classes with attributes, leading to cleaner and more maintainable code compared to traditional classes where such methods would have to be manually defined .
Decorators in Python are a design pattern that allows additional functionality to be added to an existing function or method dynamically. They are applied using the @ symbol above a function, transforming it without modifying its actual code. Decorators enable behaviors such as logging, authorization, and memoization without altering the original function .
The Global Interpreter Lock (GIL) in Python prevents multiple native threads from executing Python bytecodes concurrently, which can hinder multi-threaded Python programs from achieving parallel execution on multi-core systems. It ensures only one thread executes in the interpreter at a time, leading to underutilization of multiprocessor CPU architectures in Python .
PEP 8 is the style guide for Python code, providing conventions about how to format Python code for maximum readability. Following PEP 8 is important as it promotes a common coding standard among developers, which helps in understanding and maintaining code, especially in collaborative environments .
Generators should be preferred when working with large datasets or streams where memory efficiency is crucial. They yield items one at a time and maintain their state between iterations, thus not holding the entire dataset in memory. This makes them ideal for processing real-time data or implementing scenarios like iterating over large files .
The __init__() method in Python classes is a special initializer method that is automatically called to allocate memory when a new object/instance of a class is created. It allows initialization of the instance's attributes and can accept arguments to set the state of the object during instantiation .
In Python OOP, @staticmethod is used for methods that don't modify object or class state; they behave like regular functions. @classmethod affects the class as a whole, allowing it to access and modify class state through its second argument, which is the class itself. Instance methods operate on object level, interacting with the instance-specific data through 'self', influencing instance state. All three provide flexibility in how methods interact with class and object data .
In Python, mutable types, like lists and dictionaries, allow modification in place, which can lead to more efficient memory usage as they avoid the creation of new objects for changes. Immutable types, such as tuples and strings, do not allow changes to their contents after creation, promoting reliability by avoiding unexpected alterations, but may be less memory optimal for operations requiring frequent changes .
List comprehensions in Python provide a concise way to create lists, allowing for code that is both efficient and more readable by reducing the need for explicit loops. They can lead to fewer lines of code and optimize the speed of list creation by executing at C speed .