Python Programming Interview Questions and Answers
Python Interview Questions and Answers:
There is a list of the most important Python Programming Interview Questions and Answers.
1. What is PEP 8?
Ans – PEP stands for Python Enhancement Proposal. It is a coding convention and it is a set of recommendations about how to write your Python code more readable.
2. Define global and local variables in Python?
Ans – In Python, The variables that declared outside the function that says global variables. In Python, The variables declared inside a function that says local variables.
3. How many types of loop statements?
Ans – In Python, there are two types of loop statements are exists:
4. How memory management is done in Python?
Ans – In Python, The private heap space manages the memory. All Python objects and data structures are located in a private heap. Python Interpreter itself takes care of this private heap.
5. What are the modules in Python?
Ans – The module defines as a file that includes a set of various functions and Python statements.
6. What are the built-in types available in Python?
Ans – In Python, there are four types of built-in types are available:
2. Complex numbers
3. Floating-point numbers
7. Difference between .py and .pyc files.
.py files are Python source files,
.pyc files are the compiled bytecode files
8. Define String in Python.
Ans – The string is an immutable sequence data type. It is the sequence of Unicode characters wrapped inside single, double, or triple quotes.
9. Define slicing in Python.
Ans – A substring of a string is called a slice. Python Slice Operation is used to refer to sub-parts of sequences and strings. The subset of a string from the original string by using [ ] operator is known as Slicing Operator.
10. How do you create a Python function?
Ans – Function blocks start with the keyword def.
def function_name(parameters): <statement>
11. Difference between List and Tuple in Python.
|1. Lists are mutable||1. Tuples are immutable|
|2. Lists are slower than tuples.||2. Tuples are faster than list.|
12. What is lambda function in Python?
Ans – Lambda Function is throw-away functions, they are just needed where they have been created and it can be used anywhere a function is required. It’s just created by using the lambda keyword.
13. Difference between Xrange and range?
Ans – Xrange returns the xrange object, range returns the list and uses the same memory.
14. What is
Ans – Every package in Python is a directory that must have a special file called
_init_.py. This file may not even have a single line of code. It’s simply added to indicate that this directory is not an ordinary directory and contains a python package.
15. Define docstring in Python.
Ans – In Python, Docstrings is also known as Documentation Strings. It is very important as they help tools to automatically generate online or printed documentation. It also helps users and readers of the code to interactively browse through code.
16. How you can convert a number to a string?
Ans – To convert a number to a string, use the str() built-in function. It has the following syntax:
17. How do we reverse a list in Python?
list.reverse() is used to reverse the objects of a list.
18. How do we convert the string to lowercase?
Ans – To convert a string to lowercase, lower() function can be used here.
19. Define package in Python.
Ans – In Python, A package is a hierarchical file directory structure that has modules and other packages within it. Python Packages are searched for in the path specified by
20. Define filter(), map(), reduce() in Python.
filter(): The filter() function constructs a list from those elements of the list for which a function returns True.
map(): The map() function applies a particular function to every element of a list.
reduce(): The reduce() function returns a single value generated by calling the function on the first two items of the sequence, then on the result and the next item and so on.