Python Interview Questions
Category: Posted:Mar 15, 2017 By: admin1.What is Python?
Wikipedia defines Python as “a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.”
It is essentially a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of python include simplicity, ease of use, portability, extensibility, and availability of built-in data structures.
2. What is “PEP 8”?
“PEP 8” is a coding convention. It is a set of recommendations on making Python code more readable.
3. Define “Pickling” and “Unpickling”.
The “Pickle” module converts any Python object into a string representation and dumps it into a file using the “dump” function. This process is called “Pickling”. “Unpickling”, on the other hand, is the process of retrieving original Python objects from the stored string representation.
4. How is Python interpreted?
Python is an interpreted language. Python programs run directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language before getting executed.
5. What is the process of managing memory in Python?
Python memory is managed by the Python “private heap space”. All Python objects and data structures are located in a “private heap”.
Python also has an inbuilt “garbage collector”, that recycles all the unused memory, frees it up, and makes it available to the “heap space”.
6. Name the tools that can be used to identify bugs or perform static analysis in Python.
- “PyChecker” is a static analysis tool that detects the bugs in Python source code and sends out warnings around the style and complexity of the bug.
- “Pylint” is a tool that verifies whether the module meets accepted coding standards.
7. What is the difference between a “list” and a “tuple”?
A “list” is mutable but a “tuple” is not. Tuple can also be “hashed” for let’s say a key for dictionaries.
8. What is a “Python decorator”?
A “Python decorator” is a specific change that can be made in the Python syntax to alter functions with ease.
9. What are “Dict” and “List” comprehensions?
They are syntax constructions to ease the creation of “Dictionary” or “List” based on existing “iterable”.
10. How are “arguments” passed in Python?
Everything in Python is an “object” and all “variables” hold references to the objects. The value of the “references” are in accordance to “functions”. As a result of this, you cannot change the value of the “references”. However, you can change the “objects” if they are mutable.
11. List the built-in “types” that Python provides?
Mutable “types”:
- List
- Sets
- Dictionaries
Immutable “types”:
- Strings
- Tuples
- Numbers
12. What is “namespace” in Python?
In Python, every new “name” has a place where it resides. This location is known as “namespace”.
13. Define “lambda” in Python.
It is a single expression anonymous function often used as an inline function.
14. Why is it that “lambda forms” do not have statements?
“lambda forms” in python do not have statements since they are used to create new “function” objects and then return them during runtime.
15. Define “pass” in Python.
“Pass” is a no-operation Python statement. It is essentially a place holder in the compound statement which should be left blank.
16. Define iterators.
In Python, “iterators” are used to iterate a group of “elements” or “containers”.
17. What is a “unittest” in Python?
The unit testing framework of Python is known as “unittest”. It supports the sharing of setups, automation testing, shutdown code for tests, aggregation of tests into collections, among others.
18. Define “slicing”.
“Slicing” is a mechanism to select a range of items from sequence types like list, tuple, strings etc.
19. What are generators in Python?
Generators are the means to implement iterators. It is a normal function except that it yields “expression” in the “function”.
20. Define “docstring”.
“Docstring” is a Python documentation string. It is the means to document Python “functions”, “modules” and “classes”.
21. How do you copy an object in Python?
copy.copy () or copy.deepcopy()
22. What is a “negative index”?
Python sequences can be indexed as positive and negative numbers. For ac positive index, 0 is the first number, 1 is the second etc. For a negative index, (-1) is the last index and (-2) is the second etc.
23. How do you convert a number into a string?
By using the inbuilt function str().
24. What is the difference between “Xrange” and “range”?
“Xrange” returns the “Xrange” object while range returns the “list” irrespective of the size of the “range”.
25. Define “module” and “package”.
Each Python program file is a “module”, which imports other modules like “objects” and “attributes”.
A Python program folder is a “package” of “modules”. A package can have “modules” or “subfolders”.