Python Interview Questions – Frequently Asked
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.
- Is Python a Programming language or a scripting language?
Python is capable of scripting; however, it is considered a general-purpose programming language.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- 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
- 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.
- 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.
- 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.
- Write a program in Python to produce Star triangle.
for x in range(r):
* * *
* * * * *
- 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
print("The requested series is",f)
for x in range(2,a):
- Write a program in Python to check if a number is prime.
for x in range(2,a):
enter number 3
- Write a program in Python to check if a number is a Palindrome.
print("Not a Palindrome")
- 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
- 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):
[3, 5, 6, 7, 32, 54, 87]
- Write a Python program to check an Armstrong number.
num = int(input("Enter a number: "))
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")
- 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.
- 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
- 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 //.
- 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.
- 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.