Python Interview Questions With Answers
Python Interview Questions With Answers
What is set?
Set is created by using { } symbol.
Set stores elements in random order fashion.
Set doesn’t allow duplicate elements.
What is a dictionary?
Dictionary is used to store key-value pairs.
Keys should be unique, and values can be duplicate.
Eg:
x={
‘email’ : ‘enquiry@techpalle.com’,
‘pw’ : ‘abcd123’,
‘course’ : ‘python’,
‘duration’ : 90
}
How will you create array in python?
Python does not have built-in support for Arrays, but Python
Lists can be used instead.
However we can create array in python by importing array
module.
Eg:
Import array
arr = array.array( ‘i’ , [10,20,30,40] )
Eg:
Def fun(x,y,z=10):
print(x,y,z)
fun(1,2)
Eg:
Def f(**x):
print(x)
Eg:
fun = lambda x, y : x + y
print( fun(10,20) )
What is a regex or regular expression?
Regular experession is a string pattern that we want to find
in a given text.
what is re?
re stands for regular expression. re is a predefined
module in python which contains several
predefined regular expression functions which
programmer can use.
What is a generator?
Generator is used create an iterator (of values) which we
can iterate and read elements one by one using a loop.
What is split?
By using split function we can split a string into words
and store into a list.
Split() function expects a separator as parameter,
based on which you want to split the string. A
separator can be a space or any character.
Eg:
Eg:
def wrapper(fun):
def inner():
print(‘hello’)
fun()
print(‘bye’)
return inner
@wrapper
def display():
print(‘display function’)
1. Class
2. Object
3. Encapsulation
4. Abstraction
5. Inheritance
6. Polymorphism
What is a class
class is a virtual entity or a model which is used to create object.
class eg: class Student:
print(‘this is student class’)
What is an object
Object is a real entity.
object eg:
s1 = Student()
What is constructor or init() ?
by using constructor we can assign data into objects or we can
initialize objects
Eg for constructor::
class Bank
{
def __init__(self )
{
}
}
What is encapsulation?
binding logically related data and functions together in
one class is known as encapsulation.
class Doctor:
def suggestMedicine(self):
print(‘doctor will suggest medicine to patient’)
def doSurgery(self):
print(‘doctor will do surgery’)
Eg:
class Bank:
def addaccount(self):
//code for adding new account in the bank
class HdfcBank(Bank):
def rateofinterest(self):
//code for calculating rate of interest
Does python support method overloading?
No
class Animal:
def run(self):
print(‘animal runs on 4 legs’)
Class Human(Animal):
def run(self):
print(‘human runs on 2 legs’)
What is polymorphism?
when an entity is appearing with the same name in different
forms then that entity is said to exhibit polymorphism
Instance methods will have self as Class method will cls as parameter,
parameter, with which we can access with which we can access class
instance variables of the class. variables.
For accessing instance methods we For accessing class methods we need
need to use object. to use class name.
What is the parent most class for all classes?
object class
How will you access, members of other modules?
By using import keyword we can import one module into
other module and access its members.
eg:
@abstractmethod
public abstract void fun(self):
pass
What is an abstract class?
def: if a class contains one or more abstract methods
then it is called as abstract class.
An abstract class must inherit predefined class ABC.
Eg:
class xyz(ABC):
@abstractmethod
def f1(self):
pass
What is is ABC?
ABC stands for abstract base class.
ABC is a predefined class in abc module of
python. We need to inherit ABC class to make
a class as abstract class.
What is abc?
It is a predefined module in python which
contains ABC class.
we need to import abc module to use ABC class.
When to use abstract method?
If you know only method name but you don’t know
the logic, then we make that method as abstract
method.
Can we create object for an abstract class?
No
How do you use an abstract class?
We use an abstract class by inheriting into another
class, and by over riding all abstract methods of
parent class in the child class.
Does python support multiple inheritance of
classes?
yes
What is abstraction?
class Bank(ABC):
@abstractmethod
def deposit(self, amount):
pass
@abstractmethod
@withdraw(self, amount):
pass
In the above class we are only showing what all the methods bank class is
having, but we are hiding the actual code how we can deposit or withdraw
money. Hence above class is an example for abstraction.
What is an exception?
Exception is a run time error which terminates
the program abruptly.
How do you handle exceptions?
By using try-except blocks
Can we have multiple except blocks for one try
block?
Yes
What is finally block?
Finally block code will be executed in all the
scenarios no matter whether exception occur or
does not occur.
What is the use of finally block?
Finally block is mainly used for closing files, and
closing database connections.
One try block can have how many finally blocks?
Maximum one
Is it possible to have a try block without except
block?
Yes we can have try without except blocks in case if
finally block is available.