Python Interview Questions


Python is a very popular language and its popularity is growing. Expectations of the interviewer are also growing. In this article, we will learn commonly asked python Question and its answers.

Python Interview Questions

Question1: What is python?
Answers:  Python is a general-purpose purpose programming language which is used to develop the web application, microservice as well as machine learning models.

 

Question 2: Is python use compiler or an interpreter to compile the code?
Answer: Python use Interpreter which compiles the code line by line and then after successfully compile of whole code it then converts the code into the machine executable code.

 

Question 3: What are Lists and Tuples in python?
Answer: List and tuple both are the datatypes in Python which is used to store information in Key-value pair. They are used to store any type of data.

Key differences are:
1. List data can be modified but tuple is immutable
2. The list is slower than the tuple

 

Question 4: what is a deep and shallow copy in python?
Answers: There are 2 ways to copy an object in python
1. Shallow copy: In this method, newly created object will store the address of parent object so if there is any change in data on any object it will be reflected in all object which is created by shallow copy. The syntax for shallow copy nebobj= obj.copy(parentobj)
2. Deep copy:  In a deep copy actual values of the parent object is copied into the newly created object so, any change in the child object will not affect the values of parent object. The syntax for deep copy newlyobject = copy.deepcopy(parentobj)

 

Question 5: Memory management in the python program
Answer: Python use private heap memory to the store of object and data structures. A developer/ Programmer cannot access this heap memory. This memory is used by the interpreter.
Python has inbuild carbage collector which is used to manage unused object which is consuming the memory.

 

Question 6: What is lambda in python?
Answer: A lambda function is used to create a function with no name or we can say lambda functions are used to create the anonymous functions.

 

Question 7: What is Flask?
Answer: Flask is a micro web framework which is used to create micro services-based application we can build the whole application on the flask as well.

 

Question 8: What is *args and **kwargs in python?
Answer:  Both keywords are used to pass an argument in python. Let’s understand them by example.
*args is used to pass arguments in a function when we do not know how many arguments will be passed to the function

Sample Example:

### Declare a function
def fun(*args):  
 for arg in args:      
  print(arg) ### Call a function

fun("1 arg","2 arg","3 arg")  

###**Kwargs is used to pass variable length keyword arguments it mainly used to pass dictionary data.
Sample Example:### Declare a function

def fun(**kwargs):  
 for key, value in kwargs.items():      
   print (" {0} {1}".format(key, value))
### Call a function

fun(key1 ='value1', key2 ='value2', key3='value3')

 

Question 9: What is a pickle and unpickle in Python?
Answer: pickle is used to serialize an object in python. Pickle module of python take any kind of object and dump it to a file for further use whereas retrieving data from that file is known as unpickling. This is generally used in machine learning where we train our model on specific data and then pickle it and use this pickle file in our production env.

 

Question 10: What is Map function in python?
Answer: Map function is used to iterate on a function and its arguments should be iterable

Example: map( functionname, arguments)

Sample Code:

### Declare a function
def fun(a,b):
 return a+b

### Call a function

x= map(fun,("orange","grapes"),("banana","Mango"))
print(x)
print(list(x))

 

Question 11: What is Inheritance and it is used in python with the example?
Answer: Inheritance is one of the fundamental concepts of OOPS, in this one class can inherit function and a member variable of another class.

Example:

class parentclass(object):
#Declare constructor in class

 def __init__(self,name):
  self.name= name

 def printname(self):
  return self.name

# Declare another class

class ChildClass(parentclass):
 def childfun():
  print('child class object')

# Creating Object of parent class

par= parentclass("Bit array")
print(par.printname())

#Creating an object of child class by passing the object of the parent class
childobj =  ChildClass("Child Object")

#Call Parent class by child class object

print(childobj.printname())
# call child class own object

childobj.childfun()

 

Question 12: What is local and global variables?
Answer: Variable which is declared inside a function and whose scope is limited to the function is known a local variables.
Global variables: Variables which is not declared inside a function and can be accessed inside function is known as global variables

Example:
#declare a global variable
_globalvariable = 10
def fun():
 #declare a local variable
 _localvariable = 20

print("local variable {0}  Global variable {1}".format(_localvariable,_globalvariable))
#Calling the function

fun()

 

Question 13: What is the use of // operator?
Answer: This is known as floor division Number is a whole no when we divide with floor operator in case of division operator it provides float no

print(10/4)
print(10//4)
print(10/-4)

 

Question 14: What is multithreading and multiprocessing in python?
Answer: In multithreading, multiple programs (Threads) lives in a single processor and share the same memory space of the same processor.
In multiprocessing, Python creates a separate task for each program and assign it to different processors.

In python, multiprocessing work better then multithreading

 

Question 15: What is GIL in python?
Answer: GIL is known as global interpreter Lock That allows one thread to take control of interpreter at a time.

Due to GIL multithreaded program will not work as efficiently as it should be, Although the program is multithreaded at one moment single thread will execute.

 

Question 16: Why python use GIL? Or what problem it solves in python
Answer: every Object created in python has a reference count variable which keeps tracking the no of references that point to that object. This count variable is used by Garbage collector to manage memory if that count is zero means this variable not in use and memory is released from this object.