Top 25 Python Interview Questions & Answers


1) What is Python? What are the benefits of using Python?

Ans. Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open source.

 

2) What is pickling and unpickling?

Ans. Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.

 

3) How Python is interpreted?

Ans.  Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed.

 

4) What is PEP 8?

Ans.   PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.

 

5) What is pass in Python?

Ans. Pass means, no-operation Python statement, or in other words it is a place holder in compound statement, where there should be a blank left and nothing has to be written there.

 

6) In Python what are iterators?

Ans. In Python, iterators are used to iterate a group of elements, containers like list.

 

7) What is unittest in Python?

Ans. A unit testing framework in Python is known as unittest. It supports sharing of setups, automation testing, shutdown code for tests, aggregation of tests into collections etc.

 

8) In Python what is slicing?

Ans. A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.

 

9) What are generators in Python?

Ans. The way of implementing iterators are known as generators. It is a normal function except that it yields expression in the function.

 

10) What is docstring in Python?

Ans. A Python documentation string is known as docstring, it is a way of documenting Python functions, modules and classes.

 

11) How memory is managed in Python?

Ans. Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter takes care of this Python private heap.

The allocation of Python heap space for Python objects is done by Python memory manager. The core API gives access to some tools for the programmer to code.

Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.

 

12) What are the tools that help to find bugs or perform static analysis?

Ans. PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.

 

13) What are Python decorators?

Ans. A Python decorator is a specific change that we make in Python syntax to alter functions easily.

 

14) What is the difference between list and tuple?

Ans. The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed for e.g as a key for dictionaries.

 

15) How are arguments passed by value or by reference?

Ans. Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result you cannot change the value of the references. However, you can change the objects if it is mutable.

 

16) What is Dict and List comprehensions are?

Ans. They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.

 

17) What are the built-in type does python provides?

Ans. There are mutable and Immutable types of Pythons built in types Mutable built-in types

• List

• Sets

• Dictionaries

- Immutable built-in types

• Strings

• Tuples

• Numbers

 

18) What is namespace in Python?

Ans. In Python, every name introduced has a place where it lives and can be hooked for. This is known as namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get corresponding object.

 

19) What is lambda in Python?

Ans. It is a single expression anonymous function often used as inline function.

 

20) Why lambda forms in python does not have statements?

Ans. A lambda form in python does not have statements as it is used to make new function object and then return them at runtime.

 

21) Explain how to delete a file in Python?

Ans. By using a command os.remove (filename) or os.unlink(filename)

 

22) Explain how can you generate random numbers in Python?

Ans.To generate random numbers in Python, you need to import command as

import random

random.random()

This returns a random floating point number in the range [0,1)

 

23) Explain how can you access a module written in Python from C?

Ans. You can access a module written in Python from C by following method,

Module = =PyImport_ImportModule("<modulename>");

 

24) Mention the use of // operator in Python?

Ans. It is a Floor Divisionoperator , which is used for dividing two operands with the result as quotient showing only digits before the decimal point. For instance, 10//5 = 2 and 10.0//5.0 = 2.0.

 

25) Mention five benefits of using Python?

Ans. Python comprises of a huge standard library for most Internet platforms like Email, HTML, etc.

Python does not require explicit memory management as the interpreter itself allocates the memory to new variables and free them automatically

Provide easy readability due to use of square brackets

Easy-to-learn for beginners

Having the built-in data types saves programming time and effort from declaring variables.

 

New coming soon..