Posted in:

Top Python Interview Questions with Answers

Python is the preferred choice of language for Machine Learning and AI developers. Alongside, python is preferred for Software development also, due to its rich inherent and variety of libraries. Be it Web development or Machine Learning interview. Python interview questions are mandatory at the initial screening round. In this article, we will talk about a few Python Interview questions that are essential. 

Q1) Is Python Object Oriented Programming Language or Structural Programming language ? Mention the key benefits of Python.

Ans : Python is an object oriented Programming language with object ,modules, automatic memory management and has error handling techniques like try, except.

The most important benefit , that Python is simple and easy, with built- in Data Structures and it is open-sources. Its libraries are updated and managed by a Great group of developers.

Q2) Mention some of the in-build data structures in python.

Ans : Data Structures in Python can be broadly divided into two categories :

  • Mutable
  • Immutable. 

In Mutable in-built types , we have the following :

  • List.
  • Sets.
  • Dictionaries.

Immutable in built data types are :

  • Strings
  • Tuples

Q-3) State the differences between a module and a Package?

Ans : Each python program file can be said as a module that imports other modules like objects.

Thus, a module is a way to structure a program. The folder of a Python program is called  a package of modules.

Q-4)  Mention the Difference between  deep copy and shallow copy.

Ans: 

Deep Copy Shallow Copy
Deep copy is something that is used to store the values that are already copied Shallow copy is used when you create a new instance and it keeps the values that are copied in the new instance.
Deep copy does not copy the reference pointers to the object. It makes a reference to an object and the new object that is pointed to by some other object gets stored.  The changes that are made in the original copy  won’t affect  any other copy  that uses the objects. ‘Shallow Copy’ is used to copy the reference pointers just like it copies the value. These references point to the original object and the changes  made in any member  of the class  will also affect the original copy of it.
It slows the execution of the program due to making certain copies for each object that has been called. It allows faster execution of the program  and it also depends on the size of the data that is used.

 

Q-5) State the difference between Lists and Tuples .

Ans :  

List Tuples
Lists are mutable , which implied that it can be edited Tuples are a list which are immutable, which implies that it cannot be editable.
Syntax :  list_ex=[10,’Chelsea’,30.0] Syntax : tuple_ex=(10,’Chelsea’,30.0)

 

Q-6)  Explain Inheritance in Python with example. Which inheritance is supported by Python?

Ans : Inheritance allows one class to inherit  all the members like , (attributes and methods) of another class. Inheritance provides code reusability, makes  it easier  to create and maintain all applications. The  Class  from which we are inheriting is  called  super Class or Parent Class and the inherited class is called Derived Class or Child Class.

There are different types of inheritance supported by python. These are :

  • Single Inheritance is a kind of Inheritance where  a derived  class acquires  the members of a single Super/Parent Class.
  • MultiLevel Inheritance is a type of Inheritance in which a derived  class dc1  is inherited  from base class baseclass1 and d2 is inherited from baseclass2.
  • Hierarchical Inheritance is a kind of Inheritance from one base class, you can inherit  any number of child classes.
  • Multiple Inheritance is a kind of inheritance in which  a derived class is inherited from more than one base class.

Class ParentClass(object):

V1=”from ParentClass-v1”

V2=”from ParentClass-V2”

Class ChildClass(ParentClass):

Pass

Print(ChildClass.V1)

Print (ChildClass.v2)

Q-7)  What is a dictionary in python ? Also create a dictionary  where the key is ‘Sport_name’ and there  are 4 Sport names as values.

Ans : A dictionary is an unordered collection of elements and each element in the dictionary is stored as a key-value pair.

Ex : mydict ={ ‘Sport_name’: (‘Cricket’,’Football,’’Basketball’,’tennis)}

Q-8)  What is the lambda function ? Create a lambda function to add 10  to a given number.

Ans: A lambda function is an anonymous function which can take any number of arguments in a single expression.

Syntax : lambda arguments : expression

X=lambda b:b+10

Q-9) Write a code to read a text a .txt file and write it to a new file ‘out.txt’ in python with using inbuilt function.

Ans : with open(‘games.txt’,mode=’r’) as text_file:

               With open(‘out.txt’,mode=’w’) as out_file:

                    For line in text_file:

                            Out_file.write(line)

Q-10) what are the limitations of python ?

Ans : Python being a simple language and has a extensive source of libraries, which can do complex tasks easily, still poses some limitations :

  • It has few design restrictions.
  • It is inefficient in mobile computing
  • It consists of an underdeveloped database access layer.
  • It’s slower when compared with C  and C++ or Java.

Q-11) What is the difference between range() and xrange() function in python?

Ans : In terms of Functionality , both range() and xrange()  are  identical. Both allow  for generating a list  of integers.

The main differences between Range() and Xrange() are as follows :

Range XRange
It returns a Python List object. It returns the generator object that can be used to generate numbers only by looping. The range is displayed on demand and this technique is known as ‘lazy evaluation’
It requires much more memory and can also sometimes result in crashing. It takes less memory than range.
As it results in a list object, operations on the list can be used on it. As it returns a generator object, list operations cannot be used on it.

 

Q-12) What is a generator?

Ans : Generator in python can be defined as a function  which generates or returns an Iterator object. The Iterator object generated by generator can be used to iterate over one value at a time.

Q-13)  What is an iterator in python?

Ans :  Iterator objects in python are simply an object , which can be iterated upon. The object will return data, one element at a time.

An iterator object must contain the two methods : __iter__() and __next__(). They are collectively known as iterator protocol.

Q-14) What is iterable in python? Give Examples.

Ans : An iterable is an object in python, if  we can get an iterator from it. 

Examples : lists, tuples.

Q-15) Some companies give user random exception message and you need to reproduce it and fix. A lot of developers who prepare themselves for the interviews in big companies like Microsoft or Google use the FixException service to train their skills on fixing exceptions.

These are the few questions that are most asked in Interview, the interview can be a ‘Machine learning Interview’ or a Python Developer Interview. I have not included Machine learning questions in this article. 

For a python Developer interview, prepare well on the Flask/Django web framework. These python interview questions will give you a basic idea in understanding python and its application.