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 make 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 exist:
- while loop
- for loop
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 defined 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:
1. Integer
2. Complex numbers
3. Floating-point numbers
4. Strings
7. Difference between .py and .pyc files.
Ans – .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 used to refer to sub-parts of sequences and strings. The subset of a string from the original string by using the [ ] operator, known as the Slicing Operator.
10. How do you create a Python function?
Ans – Function blocks start with the keyword def.
def function_name(parameters):
11. Difference between List and Tuple in Python.
Ans –
1. Lists are mutable | 1. Tuples are immutable |
2. Lists are slower than tuples. | 2. Tuples are faster than list. |
12. What is a lambda function in Python?
Ans – Lambda Functions are throw-away functions, they just needed where they have been created and they 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, and range returns the list and uses the same memory.
14. What is _init_ ?
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 are 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:
str(number)
17. How do we reverse a list in Python?
Ans – list.reverse()
used to reverse the objects of a list.
18. How do we convert the string to lowercase?
Ans – To convert a string to lowercase, the lower() function can be used here.
str='WEBEDUCLICK' print(str.lower())
19. Define the package in Python.
Ans – In Python, A package is a hierarchical file directory structure that has modules and other packages within it. Python Packages searched for in the path specified by sys.path.
20. Define filter(), map(), reduce() in Python.
Ans – 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.