Python Coding Interview Questions

Avatto > > DATA SCIENTIST > > SHORT QUESTIONS > > Python Coding Interview Questions

There is a private heap space in Python that contains all the Python objects and data structures. In CPython there is a memory manager responsible for managing the heap space.
There are different components in Python memory manager that handle segmentation, sharing, caching, memory pre-allocation, etc.
Python memory manager also takes care of garbage collection by using Reference counting algorithm.
We can use Static Analysis tool called PyChecker for this purpose.

PyChecker can detect errors in Python code. PyChecker also gives warnings for any style issues.

Some other tools to find bugs in Python code are pylint and pyflakes.
In Python, Tuple and List are built-in data structures. Some of the differences between Tuple and List are as follows:
1) Syntax: A Tuple is enclosed in parentheses: E.g. myTuple  = (10, 20, “apple”);
A List is enclosed in brackets: E.g. myList = [10, 20, 30];
2) Mutable: Tuple is an immutable data structure. Whereas, a List is a mutable data structure.
3) Size: A Tuple takes much lesser space than a List in Python.
4) Performance: Tuple is faster than a List in Python. So it gives us a good performance.
5) Use case: Since Tuple is immutable, we can use it in cases like Dictionary creation. Whereas, a List is preferred in the use case where data can alter.
A Python Decorator is a mechanism to wrap a Python function and modify its behavior by adding more functionality to it. We can use @ symbol to call a Python Decorator function.
Every argument in a Python method is an Object. All the variables in Python have reference to an Object. Therefore arguments in Python method are passed by Reference.
Since some of the objects passed as reference are mutable, we can change those objects in a method. But for an Immutable object like String, any change done within a method is not reflected outside.