Python Interview Questions
1) What is the difference between a module and a package in Python?
A 1) Each Python program file is a module that imports other modules like objects. Thus, a module is a way to structure the program. The folder of a Python program is called a package of modules. know more at Python online training
Q 2) What are the built-in types available in Python?
A 2) One of the most common python interview question, There are mutable and immutable built-in types.
The mutable ones include:
- List
- Sets
- Dictionaries
The immutable types include:
- Strings
- Tuples
- Numbers
Q 3) What is lambda function in Python?
A 3) It is often used as an inline function and is a single expression anonymous function. It is used to make a new function object and return them at runtime.
Lambda is an anonymous function in Python that can accept any number of arguments and can have any number of parameters. However, the lambda function can have only a single expression or statement. Usually, it is used in situations that require an anonymous function for a short time period. Lambda functions can be used in either of the two ways:
Here’s an example of the lambda function:
a = lambda x,y : x+y
print(a(5, 6))
Output: 11
Q 4) What is meant by namespace?
A namespace refers to a naming system that is used to ensure that all object names in a Python program are unique, to avoid any conflicts. In Python, these namespaces are implemented as dictionaries with ‘name as key’ mapped to a corresponding ‘object as value.’ As a result, multiple namespaces can use the same name and map it to a different object.
Below are the three types of namespaces in Python:
- Local namespace — It includes local names inside a function. A local namespace is temporarily created for a function call and is cleared when the function returns.
- Global namespace — It consists of the names from various imported packages/ modules that are currently being used in a project. A global namespace is created when a package is imported in the script, and it lasts until the script is executed.
- Built-in namespace — It includes built-in functions of core Python and built-in names for the different types of exceptions.
Q 5 ) Explain the difference between a list and a tuple?
Any Python Interview Question and Answers guide won’t complete without this question. The list is mutable while the tuple is not. Tuples can be hashed as in the case of making keys for dictionaries.
Q 6) Difference between pickling and unpickling?
Any Python Interview Question and Answers guide won’t complete without this question. In Python, the pickle module accepts any Python object, transforms it into a string representation, and dumps it into a file by using the dump function. This process is known as pickling. The function used for this process is pickle.dump().
On the other hand, the process of retrieving the original Python object from the stored string representation is called unpickling. The function used for this process is pickle.load().
Q 7) What are decorators in Python?
A Python decorator is a specific change made in the Python syntax for the easy alteration of functions.
Q 8) Difference between generators and iterators?
In Python, iterators are used to iterate over a group of elements (in a list, for example). The way of implementing these iterators is known as generators. It yields an expression in the function, but otherwise behaves like a normal function.
Q 9) How to convert a number into a string?
One of the most common python interview questions. We can use the inbuilt str() function. For an octal or hexadecimal representation, we can use the other inbuilt functions like oct() or hex().
Q 10) What is the use of the // operator in Python?
Using the // operator between 2 numbers gives the quotient when the numerator is divided from the denominator. It is called the Floor Division operator. It is one of the general questions from the Python interview questions and answers guide. know more python training
Q 11) Does Python have a Switch or Case statement like in C?
No, it does not. However, we can make our own Switch function and use it.
Q 12) What is the range() function and what are its parameters?
The range() function is used to generate a list of numbers. Only integer numbers are allowed, and hence, parameters can be both negative and positive. The following parameters are acceptable:
range(stop)
Where ‘stop’ is the no. of integers to generate, starting from 0. Example: range(5) == [0,1,2,3,4]
range([start], stop[, step])
Start: gives the starting no. of the sequence
Stop: specifies the upper limit for the sequence
Step: is the incrementing factor in the sequence
Q 13) What is the use of %s?
%s is a format specifier which transmutes any value into a string.
Q 14) Is it mandatory for a Python function to return a value?
No
Q 15) Does Python have a main() function?
Yes, it does. It is executed automatically whenever we run a Python script. To override this natural flow of things, we can also use the if statement.
Q 16) What is GIL?
GIL or the Global Interpreter Lock is a mutex, used to limit access to Python objects. It synchronizes threads and prevents them from running at the same time. know more at Python online training from India
Q 17) Before the use of the ‘in’ operator, which method was used to check the presence of a key in a dictionary?
Ans:
The has_key() method
Q 18) How do you change the data type of a list?
To change a list into a tuple, we use the tuple() function
To change it into a set, we use the set() function
To change it into a dictionary, we use the dict() function
To change it into a string, we use the .join() method
Q 19) What are the key features of Python?
It is one of the common python interview questions. Python is an open-source, high-level, general-purpose
Since it is a general-purpose programming language and it comes with an assortment of libraries, you can use Python for developing almost any type of application.
Some of its key features are:
- Interpreted
- Dynamically-typed
- Object-oriented
- English-like syntax
Q 20) Explain memory management in Python.
A) In Python, the Python Memory Manager takes care of memory management. It allocates the memory in the form of a private heap space that stores all Python objects and data structures know more at Python online course
However, the core API allows the programmer to access some tools for coding purposes. Plus, Python is equipped with an in-built garbage collector that recycles the unused memory for the private heap space.
Q 21) What is PYTHONPATH?
A ) PYTHONPATH is an environment variable that is used to incorporate additional directories when a module/package is imported. Whenever a module/package is imported, PYTHONPATH is used to check if the imported modules are present in the existing directories. Usually, the interpreter uses PYTHONPATH to determine which module to load.