Python Core Concepts for Interviews
Python Core Concepts for Interviews
Polymorphism in Python allows functions to operate on objects of different classes if they implement a method with the same name. Method overriding, a station of polymorphism, lets a subclass provide a specific implementation for a method already defined in its superclass. This ensures the subclass can alter the method’s behavior specific to it while maintaining the interface .
Modules in Python allow for the organization and reuse of code across different scripts. Built-in modules like math and os provide additional functionality. To import a custom module, save the code as a .py file and use 'import module_name' in another script. This promotes modularity and reduces code duplication .
Opening a file in write ('w') mode truncates the file to zero length before writing, removing existing content, whereas append ('a') mode writes data to the end of the file, preserving existing content. Use write when overwriting is needed, and append when augmenting the file with new data is desired .
The 'try' block contains code that might throw an exception. The 'except' block captures exceptions, allowing for graceful error handling. The 'finally' block contains code that should run regardless of whether an exception occurred. Example: 'try: risky_code() except ValueError: handle_error() finally: cleanup()'. This allows handling only specific errors and ensures cleanup happens .
The four pillars of Object-Oriented Programming in Python are Encapsulation, Abstraction, Inheritance, and Polymorphism. Encapsulation is implemented by using private variables (prefix __) to prevent direct access and modifying them through methods. For example, a class might have a private variable '__data' and a method 'get_data()' to retrieve its value .
The 'map' function is appropriate for applying a single function to each item in an iterable, like when transforming a list of numbers. 'filter' is suitable when a subset of items needs to be extracted from an iterable based on a condition. 'sorted' should be used to create a sorted representation, such as ordering records by a specific key value .
Lambda functions in Python are anonymous functions defined with the lambda keyword. They can have any number of inputs but only one expression. The result of the expression is returned. They're often used for throwaway functions, especially when combined with map(), filter(), and sorted(). For example, 'square = lambda x: x * x' defines a simple lambda function to square a number .
Python has 'for' loops, which iterate over sequences, and 'while' loops, which repeat until a condition is false. 'for' loops are suitable for iterating over collections like lists or strings, while 'while' loops are better for scenarios where the number of iterations depends on a condition — like a countdown or reading from a file until EOF .
The primary differences are that lists are mutable, allowing changes in situ, while tuples are immutable and cannot be altered after creation. Lists are used when elements are expected to change, while tuples are suited for static collections where immutability is desired for safety or performance reasons .
Python is dynamically typed, meaning there is no need for explicit declaration of variables. The common types used include int, float, str, bool, list, tuple, set, and dict.