Python Programming Interview Questions and Answers
There is a list of top frequently asked Python programming questions and answers are given below.
1. What is PEP 8?
Ans – PEP stands for Python Enhancement Proposal. It is a coding convention and it is a set of recommendation about how to write your Python code more readable.
2. Define global and local variables in Python?
Ans – In Python, The variables that are declared outside the function are called global variables. In Python, The variables that are declared inside a function are called local variables.
3. How many types of loop statements?
Ans – In Python, there are two types of loop statements are supported:
4. How memory management is done in Python?
Ans – In Python Memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. This private heap is taken care of by Python Interpreter itself.
5. What are the modules in Python?
Ans – The module is 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, The built-in types are given as follows:
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.
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.