Python Interview Questions and Answers

Python interview questions

These Python questions have been designed for various interviews, competitive exams and entrance tests. We have covered questions on both basic and advanced concepts which will help you improve your skills to face interview questions on Python.

Who is this Python interview questions designed for?

All the Web developers, Front End developers, UI/ UX developers and designers, artificial intelligence (AI) professionals, machine learning professionals and deep learning professionals will find these questions extremely useful. All freshers, BCA, BE, BTech, MCA and college students wanting to make a career in front end designing will be highly benefitted by these questions.

Python interview questions topics

This section covers Python topics like - features of Python, how Python is interpreted, Memory management in Python, Python dictionary, Tuples in Python, pickling and unpickling etc.

1. What is Python?

Python is a high-level, interpreted and object-oriented scripting language. It is simple and easy to read and write that enables the programmer to concentrate mainly on finding solutions than on syntax. It's readable code and large library makes Python such a good choice for artificial intelligence (AI), machine learning and deep learning projects. Python is both popular and widely used. It works on every operating system such as Windows, Mac, Linux etc.

2. What are the features that make Python different from other programming languages?

Following are the important features of Python:

  • Python is an Open Source Language. It can be easily downloaded and installed on any Operating System for free.
  • Python is high level programming language. It is easy to learn and code which makes it a developer-friendly language.
  • Python supports both procedure-oriented and object-oriented programming.
  • Python is an interpreted language that makes debugging easy. It can also be compiled to byte-code for building large applications.
  • Python is dynamically-typed language and supports dynamic type checking.
  • Python offers automatic memory management that frees user from having manually allocate and free memory in the code.
  • Python offers interfaces to all major commercial databases.
  • Python offers wide selection of libraries and frameworks that helps building powerful software quickly and cost-effectively.
  • Python can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
  • Python is a portable language which can run on different platforms.

3. Explain how Python is interpreted.

Python language is an interpreted language. Python program is executed directly from the source code. The source code goes through an interpreter which turns into intermediate language first and then to the native language / machine language and then gets executed.

4. How memory is managed in Python?

Memory management in Python involves a private heap containing all Python objects and data structures. Interpreter takes care of Python heap and that the programmer has no access to it. The allocation of heap space for Python objects is done by Python memory manager.

Python also has a build-in garbage collector which recycles all the unused memory. When an object is no longer referenced by the program, the heap space it occupies can be freed.

5. What is a Python dictionary?

A Python dictionary is just like a dictionary in the real world. It is another example of a data structure that stores an unordered collection of data. The dictionary stores data as key-value pairs where each key-value pair maps the key to its associated value. A program can access any member of a directory using a key.

All keys in a dictionary must be unique. A key and its value are separated by a colon. The key, value pairs are separated with commas.

6. What are Tuples in Python? What is the difference between Tuples and Lists?

A tuple is an immutable list of Python objects. The elements of a tuple cannot be changed once they are assigned. The tuple is used in the cases where we need to store the read-only collections.

It can be written as the collection of comma-separated values enclosed with round brackets.

The difference between lists and tuple are:

  • Lists are mutable while Tuples are immutable.
  • Lists are written with square brackets while Tuples are written with round brackets.

7. Explain the //, %, and ** operators in Python.

// Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.

Example

if a = 5, b = 2 then a // b = 2

%  Modulus - Returns remainder when left hand operand is divided by right hand operand.

Example

if a = 50, b = 10 then a%b = 0

** Exponent - Performs exponential calculation on operators.

Example

a**b means a raised to the power of b

8. How does break, continue and pass work?

Break - The break keyword causes program control to exit from the loop when some condition is met.

Continue - The continue statement causes program control to skip all the remaining statements in the current iteration of the loop and returns the control to the beginning of the loop.

Pass - The Pass statement causes program control to pass by without executing any code. If we want to bypass any code pass statement can be used.

9. What is the difference between remove, del and pop in Python list?

remove: This method is used to remove the first matching value

Example

list=[1,2,3,4]
list.remove(2)

#  Updated list
[1, 3, 4]

del: This method allows to delete the elements using its index. It doesn’t return any value.

Example

list = [4,5,6,7,8]
del list[1]

#  Updated list
[4,6,7,8]

pop: This method is used to remove the element by index and returns the removed element.

Example

list = [5,6,7,8]
list.pop(1)

#  Updated list
[5,7,8]

10. What is pickling and unpickling?

When you want an object to be saved in a file and use them later or send over a network, you can use Python Pickle.

Pickling refers to the process of converting an object in memory to a byte stream that can be stored on disk or sent over a network.

Unpickling is the process of loading a pickled file back into a Python program.

11. What are negative indexes in Python?

Python programming language supports negative indexing of arrays, lists or tuples to refer elements from the end to start. This means the index value of -1 refers to the last element, and -2 refers to the second last element and so on.

Example:

arr = [4, 8, 12, 16, 20]
print (arr[-1])
print (arr[-2])

Output:
20
16

12. What is the difference between append() and extend() methods?

Both append() and extend() methods are methods used to expand lists at runtime.

Difference between append() and extend() methods:

In append, we can add single element at the end of the list. In extend, we append all the element of the iterables at the end of the list.

In append, since we add single value at the end of the list, the length of the list will be incremented by one. In extend, the length of the list will be incremented based on elements present in the iterables.

In append, we can add any list, string or number to the list. In extend, we can add only elements of iterables.

Example

>>> a = [5,10,20]
>>> a
[5,10,20]

>>> a.append("five")
>>> a
[5,10,20,'five']

>>> a.extend("six")
>>> a
[5,10,20,'five','s','i','x']

13. What are decorators in Python?

Decorator helps in extending the functionalities of existing function without modifying it. Using Decorator, we can add extra features in the existing functions.

14. What is the difference between deep and shallow copy?

Deep copy creates a new and separate copy of the object and its elements. It means that any change made in the new copy of the object do not reflect in the original one.

Shallow copy creates a copy of the object but instead of copying elements of the object to the new object, it copies the references to their memory addresses. It means any chance made to a copy of object do reflect in the original object.

15. What is lambda in Python? Why is it used?

A lambda function is a simple one line function. It can have any number of arguments but only one statement. We do not use def or return keywords like the way we use with a traditional python function. Instead, we use ‘lambda’ keyword to define a lambda functions.

Example of Lambda Function

lambda a,b:a + b

Here’s traditional python function of the above lambda function

def add(a,b):
    return a + b