Python Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. How memory management is done in Python?
- In Python memory management is done using private heap space. The private heap is the storage area for all the data structures and objects. The interpreter has access to the private heap and the programmer cannot access this private heap.
- The storage allocation for the data structures and objects in Python is done by the memory manager. The access for some tools is provided by core API for programmers to code.
- The built-in garbage collector in Python is used to recycle all the unused memory so that it can be available for heap storage area.
Ques 2. What are the differences between Java and Python?
Please refer Java vs Python.
Ques 3. Define pickling and unpickling in Python.
Pickling in Python: The process in which the pickle module accepts various Python objects and converts them into a string representation and dumps the file accordingly using the dump function is called pickling.
Unpickling in Python: The process of retrieving actual Python objects from the stored string representation is called unpickling.
Ques 4. What are Python String formats and Python String replacements?
Python String Format: The String format() method in Python is mainly used to format the given string into an accurate output or result.
Python String Replace: This method is mainly used to return a copy of the string in which all the occurrence of the substring is replaced by another substring.
Ques 5. Name some of the built-in modules in Python.
The built-in modules in Python are:
- sys module
- OS module
- random module
- collection module
- JSON
- Math module
Ques 6. What is _init_?
The _init_ is a special type of method in Python that is called automatically when the memory is allocated for a new object. The main role of _init_ is to initialize the values of instance members for objects.
Ques 7. Define generators in Python?
The way of implementing an effective representation of iterators is known as generators. It is only the normal function that yields expression in the function.
Ques 8. Define docstring in Python?
The docstring in Python is also called a documentation string, it provides a way to document the Python classes, functions, and modules.
Ques 9. How to remove values from a Python array?
The elements can be removed from a Python array using the remove() or pop() function. The difference between pop() and remove() will be explained in the below example.
Example of remove():
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits)
Output:
['apple', 'cherry']
Example of pop():
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
Output:
['apple', 'cherry']
Ques 10. How do Python arrays and lists differ from each other?
The difference between Python array and Python list is as follows:
Arrays | Lists |
The array is defined as a linear structure that is used to store only homogeneous data. | The list is used to store arbitrary and heterogeneous data |
Since array stores only a similar type of data so it occupies less amount of memory when compared to the list. | List stores different types of data so it requires a huge amount of memory |
The length of the array is fixed at the time of designing and no more elements can be added in the middle. | The length of the list is no fixed, and adding items in the middle is possible in lists. |
Ques 11. What is the difference between range and xrange?
Both methods are mainly used in Python to iterate the for loop for a fixed number of times. They differ only when we talk about Python versions.
The difference between range and xrange is as follows:
Range() method | Xrange() method |
The xrange() method is not supported in Python3 so that the range() method is used for iteration in for loops. | The xrange() method is used only in Python version 2 for the iteration in loops. |
The list is returned by this range() method | It only returns the generator object because it doesn’t produce a static list during run time. |
It occupies a huge amount of memory as it stores the complete list of iterating numbers in memory. | It occupies less memory because it only stores one number at a time in memory. |
Ques 12. What is data abstraction in Python?
In simple words, abstraction can be defined as hiding unnecessary data and showing or executing necessary data. In technical terms, abstraction can be defined as hiding internal processes and showing only the functionality. In Python abstraction can be achieved using encapsulation.
Ques 13. Define encapsulation in Python.
Encapsulation is one of the most important aspects of object-oriented programming. The binding or wrapping of code and data together into a single cell is called encapsulation. Encapsulation in Python is mainly used to restrict access to methods and variables.
Ques 14. What is polymorphism in Python?
By using polymorphism in Python we will understand how to perform a single task in different ways. For example, designing a shape is the task and various possible ways in shapes are a triangle, rectangle, circle, and so on.
Ques 15. Does Python make use of access specifiers?
Python does not make use of access specifiers and also it does not provide a way to access an instance variable. Python introduced a concept of prefixing the name of the method, function, or variable by using a double or single underscore to act like the behavior of private and protected access specifiers.
Ques 16. How can we create an empty class in Python?
Empty class in Python is defined as a class that does not contain any code defined within the block. It can be created using pass keywords and object to this class can be created outside the class itself.
Ques 17. How can we create a constructor in Python programming?
The _init_ method in Python stimulates the constructor of the class.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Output:
John
16
Ques 18. Define Inheritance in Python?
When an object of child class has the ability to acquire the properties of a parent class then it is called inheritance. It is mainly used to acquire runtime polymorphism and also it provides code reusability.
Ques 19. What are the cool things you can do with Python?
The following are some of the things that you can perform using Python:
- Automate tasks
- Play games
- Build a Blockchain to mine Bitcoins
- Build a chatbot interface combined with AI
Most helpful rated by users:
- What is Python?
- How do I share global variables across modules?
- How do you set a global variable in a function in Python?
- Why can't I use an assignment in an expression?
- What are the rules for local and global variables in Python?
Related differences
Related interview subjects
Python Pandas interview questions and answers - Total 48 questions |
Python Matplotlib interview questions and answers - Total 30 questions |
Django interview questions and answers - Total 50 questions |
Pandas interview questions and answers - Total 30 questions |
Deep Learning interview questions and answers - Total 29 questions |
PySpark interview questions and answers - Total 30 questions |
Flask interview questions and answers - Total 40 questions |
PyTorch interview questions and answers - Total 25 questions |
Data Science interview questions and answers - Total 23 questions |
SciPy interview questions and answers - Total 30 questions |
Generative AI interview questions and answers - Total 30 questions |
NumPy interview questions and answers - Total 30 questions |
Python interview questions and answers - Total 106 questions |