Master your next Python interview with these 25 essential questions and detailed answers to showcase your coding skills and knowledge.
int, float, complexstrlist, tuple, rangedictset, frozensetboolbytes, bytearray, memoryviewA list is mutable, meaning its elements can be changed, while a tuple is immutable. Lists use square brackets [], and tuples use parentheses (). Lists are slower but more flexible. Tuples are faster and used when data should not change.
PEP 8 is Python’s style guide for writing readable code. It covers naming conventions, indentation, spacing, and line length. Following PEP 8 ensures consistency across Python codebases. It improves readability, maintainability, and collaboration.
*args allows a function to accept any number of positional arguments as a tuple. **kwargs lets it accept any number of keyword arguments as a dictionary. They provide flexibility when calling functions with dynamic input. Common in decorators and wrappers.
Decorators are functions that modify the behavior of other functions or methods. They use @decorator_name syntax above a function definition. Common use cases include logging, access control, and memoization. Decorators are built on closures and higher-order functions.
copy.copy() is for shallow, copy.deepcopy() is for deep copy.[expression for item in iterable if condition][x*x for x in range(5) if x%2==0] results in [0, 4, 16]== checks if the values of two variables are equal, while is checks if they refer to the same object in memory. For immutable objects like numbers and strings, they may appear similar but are different operations. is is often used for identity checks. == is for logical equivalence.
with statement in Python?The with statement simplifies exception handling for resource management, such as file operations. It ensures that resources are properly closed after their use. It is implemented using context managers. Common usage: with open('file.txt') as f:.
lambda keyword.lambda arguments: expressionmap(), filter(), and sorted().gc module handles cyclic references.Generators are functions that return an iterator using the yield keyword. Unlike regular functions, they do not store the entire sequence in memory. They are useful for handling large data streams. Each call to next() resumes where the last yield occurred.
print() as a function; Python 2 uses it as a statement.__init__.py file.import to include them in your script.@staticmethod does not take the class or instance as its first argument. @classmethod takes cls as the first argument and can access class-level variables. Use staticmethod when method logic doesn’t need class data. Use classmethod when logic depends on class attributes.
Mutable objects can be changed after creation, while immutable ones cannot. Lists, dictionaries, and sets are mutable. Strings, tuples, and integers are immutable. This affects how variables are passed and copied in functions.
The GIL is a mutex that allows only one thread to execute in the Python interpreter at a time. It prevents true multi-core CPU execution in CPython. This limits multithreading for CPU-bound tasks. However, it's not an issue for I/O-bound operations.
try, except, else, and finally blocks.try: block to test for errors.except: block to handle the error.else: runs if no error occurs.finally: always runs, used for cleanup.Arrays (from the array module) are homogeneous and more memory-efficient. Lists can hold mixed data types and are more flexible. Arrays are used when numeric computation is critical. Lists are more commonly used for general-purpose storage.
map(), filter(), and reduce() functions.map(): Applies a function to all items in an iterable.filter(): Filters items based on a condition.reduce(): Applies a rolling computation to items (requires functools).requirements.txt to list packages.pip or pipenv for installation.venv, virtualenv) to isolate dependencies.poetry and conda also manage packages and environments.open(filename, mode).read(), write(), readlines(), etc.close() or with block.'r', 'w', 'a', 'rb', 'wb', etc.