Python Interview Questions
Python Interview Questions
What is set?
Set is created by using { } symbol.
Set stores elements in random order fashion.
Set doesn’t allow duplicate elements.
eg: c = {‘palle’, ‘python’, ‘bangalore’}
What is a dictionary?
dictionary is used to store key-value pairs.
Keys should be unique, and values can be duplicate.
eg:
d={
‘email’ : ‘[email protected]’,
‘pw’ : ‘abcd123’,
‘course’ : ‘python’,
‘duration’ : 90
}
How will you create array in python?
we can create array in python by importing array module.
eg:
Import array
e = array.array( ‘i’ , [10,20,30,40] )
print(e)
What is the difference between array and list?
array list
In array we can store only In list we can store homogeneous and
homogeneous elements. heterogeneous elements.
What is the difference between list and dictionary?
list dictionary
List is an ordered collection of elements, Dictionary is used to store pairs of
where the elements are stored on index elements
basis.
We can access list elements using index. We can access dictionary values with
keys.
Note : same difference can be told for tuple vs dictionary also.
eg:
def m2(*x):
print(x)
m2(100,200,300)
what is kwargs (key word arguments) ?
keyword arguments is represented with **
by using **kwargs programmer can pass
variable number of key worded arguments (or
pairs of elements)
eg:
def m3(**x):
print(x)
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 a regex or regular expression?
1. regular experession is a string pattern that we want
to find in a given text.
2. re is a predefined module in python which contains
regular expression functions.
eg:
import re
re.search(‘all’ , ‘palle’)
eg:
h = ‘palle technologies python bangalore’
mylist = h.split(‘ ‘)
print(mylist)
it will display output as [‘palle’ , ’technologies’ , ’python’ , ’bangalore’]
what is the use of join() function?
by using join function we can join the list of elements
into a string.
eg:
j = [‘palle’, ‘technologies’, ‘python’, ‘bangalore’]
k = ‘ ‘.join(j)
print(k)
it will display output as palle technologies python bangalore.
What is decorator?
decorator is used to modify the behavior of a function,
without explicitly changing it’s structure.
eg:
def wrapper(fun): In the above code wrapper
def inner(): is the decorator for display() function
@wrapper
def display():
print(‘display function’)
What is Object oriented programming
language?
• Any Programming language which supports
below 6 features is considered as OOP
language
1. Class
2. Object
3. Encapsulation
4. Abstraction
5. Inheritance
6. Polymorphism
What is a class
class is a virtual entity.
class acts as blue print for objects.
class is used for storing related variables and methods.
eg: class Student:
def study(self):
print(‘student is studying’)
What is an object
Object is a real world entity.
eg:
s1 = Student()
what is constructor or init() ?
by using constructor we can assign data into objects or we can
initialize objects
eg:
class Bank:
def __init__(self, name, acno):
self.name = name
self.acno = acno
b1 = Bank(‘ramesh’, 12345)
What is encapsulation?
binding logically related data and functions in a
common related place, is known as encapsulation.
eg:
class Doctor:
def __init__(self, name, exp):
self.name = name
self.exp = exp
def suggestMedicine(self):
print(‘doctor will suggest medicine to patient’)
eg:
class Bank:
def addaccount(self):
pass #contains code for adding new account in the bank
class HdfcBank(Bank):
def rateofinterest(self):
pass #contains code for calculating rate of interest
What is method overriding?
1. having same method name in parent class and child class, with
same number of parameters, is known as method overriding.
2. overriding is used for changing the behavior of parent class
method in the child class.
eg:
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?
1. when an entity is appearing with the same name in
different forms, then that entity is said to exhibit
polymorphism
2. method over riding is an example of polymorphism.
class Doctor:
def __init__(self,name,exp):
self.name = name
self.exp = exp
what is the use of super() function
by using super() function we can access parent
class methods from child class.
class A:
def m5(self):
print('hello')
class B(A):
def m6(self):
super().m5()
What is class variable?
1. If we declare a variable at class level, then it is
considered as a class variable.
2. we can use class name to access class variables.
class C:
x = 10
def display(self):
print('hello')
print(C.x)
What is the dif between instance variable and class variable?
Instance variable Class variable
Instance variables are object specific class variables are class specific
instance variables are declared with class variables are declared at the class
self keyword as prefix. level, without self keyword.
for accessing instance variables we for accessing class variables we can use
need to use object. class name.
class D:
x = 10
def __init__(self):
self.y = 20
d1 = D()
print(d1.y)
print(D.x)
What is the dif between instance method and class
method?
Instance method Class method
instance methods will have self Class methods will have cls parameter by
parameter by default. default.
Class method should be marked with
@classmethod decorator above the
method name.
For calling instance methods we need to For calling class methods we can use class
use object. name.
class E:
def m7(self): e1 = E()
print('instance method') e1.m7()
E.m8()
@classmethod
def m8(cls):
print('class method')
What is dif between static method and class method?
Static method Class method
class F:
@classmethod
def m9(cls):
print('class method')
@staticmethod
def m10():
print('static method')
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.
class G:
def __init__(self):
self.__x = 10 #private variable x
def m11(self):
print(self.__x) #we can access same class
g1 = G()
print(g1.__x) #error we can’t access private variable outside class
How will you create protected variable in
python?
1. If we use 1 underscore before a variable or
method, then it is considered as protected
member.
2. Protected members can be accessible only
with in the same class, as well as in the child
class.
eg: _y = 20 #here y is considered as protected
eg:
@abstractmethod
def fun(self):
pass
what is an abstract class?
1. if a class contains one or more abstract methods then
it is called as abstract class.
2. an abstract class must inherit from predefined class
ABC.
3. we can’t create object for an abstract class.
eg:
from abc import ABC
class H(ABC):
@abstractmethod
def f1(self):
pass
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 is ABC?
1. ABC stands for abstract base class.
2. ABC is a predefined class of python. we need
to inherit from ABC class, to mark a class as
an abstract class.
what is abc?
1. It is a predefined module in python which
contains ABC class.
2. we need to import abc module to use ABC
class.
What is abstraction?
hiding implementation details and exposing the
required details to the user, is known as abstraction.
eg :
try:
#code that gives error
except errorname:
#error storing code
try:
x = [10,20,30]
print(x[3])
except IndexError:
print('trying to access invalid position')