[2022] Python Interview Questions – Frequently Asked

Written by

Vaaruni Agarwal

Python is an interpreted general-purpose programming language, that was developed by Guido Van Rossum in 1991. Python is one of the most widely used programming languages of 2020. One of the reasons being that, python has a wide range of libraries, which could be used to implement different functionalities in the fields of AI, ML, Data Science, Web Development, etc.

  1. Is Python a Programming language or a scripting language?

Python is capable of scripting; however, it is considered a general-purpose programming language.

  1. What is an interpreted language?

An interpreted language is a programming language, which is not in machine-level code before runtime. Thus, this kind of language needs to be interpreted by an interpreter first so that it can be converted in a form understood by machines.

  1. What is pep 8?

PEP or the Python Enhancement Proposal is a set of rules that specify how to format Python code for maximum readability.

  1. How is memory managed in Python?

Memory management in python is done by Python heap. All the objects and data structures are located in a heap. This allocation is done by the memory manager. The inbuilt garbage collector recycles the unused memory.

  1. What is a namespace?

A namespace is a system to have a unique name for an object in Python. An object might be a variable or a method. Python itself maintains a namespace in the form of a Python dictionary.

  1. What is PYTHONPATH?

PYTHONPATH is an environment variable which is used whenever a module is imported. It is checked for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.

  1. What are modules?

Modules are files, which contain some code used to perform an operation. This code can either be functions classes or variables. A Python module is stored with the extension of .py. This, .py file contains the executable code.

  1. What is encapsulation in Python?

Encapsulation is a concept of OOPs. This prevents data from being directly modified. In Python, the private attributes are denoted using an underscore as the prefix.

  1. What do you mean by data abstraction in Python?

Data Abstraction, another OOPs concept has the methodology of providing only the required details and hiding the implementation from the others. Data Abstraction can be achieved very easily with the help of interfaces and abstract classes in Python

  1. What is the difference between the local and the global variables?

Global Variables: The variables that are declared outside a function or in global space are called global variables. These variables can be accessed anywhere in the program.

Local Variables: Variables declared inside a function or within scope are known as local variables. They have a limited scope.

11. Is python a case sensitive language?

Yes. Python is a case sensitive language.

12. Who is the creator of Python?

Guido Van Rossum created the Python language in 1991.

  1. What is the use of indentation in Python?

Indentation is used to specify a block of code. All code within loops, classes, functions, is specified within an indented block. It is usually done using four space characters. Thus, if the Python code is not indented then errors
are thrown.

  1. What are functions in Python?

A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used. The advantage of creating a function is that it makes the code cleaner, and ensures re-usage of the same
piece of code. Also, it is in line with the DRY principle.

  1. What do you mean by __init__?

 __init__ is a method or constructor in Python. It is automatically called and is used to allocate memory when a new object or an instance of a class is created. All classes must have the __init__ method.

  1. What is a lambda function?

An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement. Lambdas are used whenever the code to be enclosed in functions are simple and can be squeezed
into a single line of code.

  1. What is self in Python?

Self is an instance or an object of a class and is explicitly included as the first parameter. Self helps to differentiate between the methods and attributes of a class with local variables.

  1. What are the loop control statements in Python?

There are three loop control statements in Python:

:        Break is a conditional control statement, it terminates the normal flow of execution and the control is transferred to the next statement after the loop body.

Continue: Continue is another conditional control statement. In this case the control is transferred to the next iteration of the loop.

Pass: To skip the execution part of any loop the pass statement is used.

  1. What are iterators in python?

 Iterator is an object that can be iterated upon. An object which will return data, one element at a time is commonly referred as an iterator in Python.

  1. What is the difference between range & xrange?

Range and xrange both provide a way to generate a list of integers, however range returns a Python list object and xrange returns an xrange object. If the range for generating a list is long then, xrange is used.

  1. What is pickling and unpickling?

Pickle accepts a Python object and converts it into a string representation and dumps it into a file by using the dump function. This whole process is called pickling. The reverse process of pickling is known as unpickling.

  1. What are the generator functions in python?

A generator function is used to generate a value. It is recognized by the, yield keyword. This yield keyword replaces return in a function, and the corresponding function is known as the generator function.

  1. What are docstrings?

Docstrings or the documentation strings are characterized by triple quotes. They are not assigned to any variable and thus sometimes act as comments as well.

  1. What is the usage of in operator in Python?

is a membership operator in Python. It is used to test whether a value or variable is found in a sequence. This may be a string, list, tuple, set or a dictionary. The complimentary operator is the
not in operator. It returns the result as true or false.

  1. What is the help() function in Python?

The help() function is used to display the documentation string. Apart from this the help function also allows to see the help related to the modules, keywords, attributes of Python.

  1. What is a Python dictionary?

Dictionary is one of the built-in datatypes in Python. A dictionary defines one-to-one relationship between keys and values. It contains a pair of keys and the corresponding values for the same.

  1. What is a ternary operator?

A Ternary operator is used in place of the conditional statements. It is denoted by a ? Ternary operator, evaluates on three operands, thus the name. It checks for a condition, and on the basis of the same gives an output.

  1. What is the: *args, **kwargs in Python?

*args is used the number of arguments to be passed to a function are not fixed, or list or tuple of arguments, needs to be passed to a function.

**kwargs is used when a dictionary must be passed as keyword arguments.

  1. What are negative indexes in Python?

Sequences are indexed in Python and these indexes could either be positive or negative. The positive indexes refer to the elements from the starting of the sequence, while the negative indexes are used to refer to the elements from
the ending of a sequence.

  1. What are packages in Python?

A package in Python is a directory. This directory, must contain a special file called __init__.py .  A package is a collection of modules.

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

A Shallow copy is used when a new instance type gets created and it keeps the values that are copied in the new instance. Shallow copy is used to copy the reference pointers just like it copies the values.

Deep copy on the other hand is used to store the values that are already copied. It makes the reference to an object and the new object that is pointed by some other object gets stored.

  1. How is Multithreading achieved in Python?

The multithreading in Python is carried out with the help of Global Interpreter Lock (GIL). It ensures that only one of the threads are executes at a time. A thread takes upon the GIL, executes itself and, then passes the GIL onto
the next thread.

  1. What is the process of compilation and linking in python?

The processes of compiling and linking allows the compilation of the new extensions without any error. Linking process starts after compilation is complete. The python interpreter can provide the dynamic loading of the configuration
setup files

  1. What are Python libraries?

A library in Python is actually a collection of the packages in Python. It is because of these libraries that various functionalities such as that of the AI, ML, Data Science are implemented in Python. Some popular python libraries
are – Numpy, Pandas, Matplotlib, Scikit-learn, etc.

  1. Explain the concept of Inheritance in Python.

Inheritance, is OOPs concept, it allows code reusability, makes it easier to create and maintain an application. The class from which new class is inherited is called a parent class and the new class is called as a child class in

  1. What is monkey patching in Python?

The monkey patch only refers to the dynamic modifications which are performed on a class or module at the run-time. The process to monkey patch a class or module is known as monkey patching in Python.

  1. What is Polymorphism in Python?

Polymorphism means the ability to take multiple forms. Polymorphism is also an OOPs concept. It makes sure that a method can take multiple forms.

Eg: if the parent class has a method named ABC then the child class also can have a method with the same name ABC having its own parameters and variables.

  1. How to create an empty class in Python?

It is possible to create an empty class in Python. An empty class does not have any code defined within its block. The pass keyword can be used for this purpose.

  1. Write a program in Python to produce Star triangle.
def pyfunc(r):

    for x in range(r):

        print(' '(r-x-1)+''(2x+1))    




        * * *

      * * * * *

  1. Write a program to print a Fibonacci series in Python.
a=int(input("Enter the terms"))

f=0   #first element of series

s=1   #second element of series

if a<=0:

    print("The requested series is",f)


    print(f,s,end=" ")

    for x in range(2,a):


        print(next,end=" ")




  1. Write a program in Python to check if a number is prime.
a=int(input("enter number"))    

if a>1:

    for x in range(2,a):


            print("not prime")





    print("Not Prime")


enter number 3


  1. Write a program in Python to check if a number is a Palindrome.
a=input("enter number")


if a==b:



    print("Not a Palindrome")


enter number



  1. Write a Python program to swap two numbers without using 3 variables.
a = int(input(‘Enter first number’))

b = int(input(‘Enter second number’))

a,b = b,a



Enter first number


Enter second number


10  20

  1. Write a Python program to execute the Bubble sort algorithm.
def bs(a):           # a = name of list

    b=len(a)-1       # minus 1 because to compare 2 adjacent values                          

    for x in range(b):

        for y in range(b-x):

            if a[y]>a[y+1]:


    return a




[3, 5, 6, 7, 32, 54, 87]
  1. Write a Python program to check an Armstrong number.
num = int(input("Enter a number: "))

initialize sum

sum = 0

find the sum of the cube of each digit

temp = num

while temp > 0:

   digit = temp % 10

   sum += digit ** 3

   temp //= 10

display the result

if num == sum:

   print(num,"is an Armstrong number")


   print(num,"is not an Armstrong number")

  1. What is map function in Python?

The map() function executes a specified function for each item in a iterable. The item is sent to the function as a parameter.

  1. Which one is better python numpy or the lists?

Python numpy is considered better that the lists, because of the following reasons:

  • Less Memory
  • Fast Speed
  • Convenience of usage

  1. What is the usage of floor division operator in Python?

Floor division operator is used to give an accurate result of the quotient of two operands. Whenever, a normal division operator is used then, the result is rounded off to the nearest whole number. However, to get an accurate value
a floor division operator is used. It is denoted by //.

  1. What is the usage of split() method in Python?

Split method, as the name suggests is used to split or break a string in Python. This method splits the given string into a list. A separator can be specified explicitly, however, it takes a whitespace as a default separator to
split the strings.

  1. What is multiple inheritance?

Multiple inheritance is a type of inheritance, which specifies that a class can be derived from more than one parent classes. This means that a class can have more than one parents according to the concept of multiple inheritance.

[2022] Python Interview Questions &#8211; Frequently Asked