Python Developer Interview Questions Answers |
Python Developer Interview Questions Answers
1. what is the Difference Between List and Tuple Explain?
Ans-LIST
1. list is mutable
2. List is like container to contain different types of objects
3. syntax-
list=['a','b','c',1,2,3]
4 List iteration is slower
5 List consume more memory
6 operations like insert and delete are better.
Tuple
1. tuple is immutable
2. Tuple contain immutable object
3. syntax
tuples=('a','b','c',1,2)
4. It is faster than the list
5. It consumes less memory
2. What is a Decorator? Explain it with an example.
Ans- A decorator is a function that takes another function as an argument.it works
without altering the source code of the original function that you passed in.
def first(msg):
print(msg)
first("BYE")
second = first
second("BYE")
output
BYE
BYE
3. Tell me the Difference Between List Comprehension and Dict Comprehension
Ans- [expression for an item in iterable if condition]
Dict Comprehension
{key:value for(key,value) in iterable if condition conditional}
4. Can you explain How is Memory Managed In Python?
Ans- It is managed by private Heap Memory and space Allocation
5. explain the Difference Between Generators and Iterators
Ans-Generator
It is an iterator that executes only once
It uses the "yield" keyword
It is mostly used in loops
Iterators
An Iterator is a type of object which contains a countable number of values.
it uses iter() and next() function
every Iterator is not the generator
6. What is the 'init' keyword in python?
Ans- The _init_ method is similar to constructors. It is used to initialize an object
example
class Person
def_init_(self,name):
self.name=name
7. Difference Between Modules and Packages in Python
Ans-Modules is a simple python file that contains function and global variable
having a .py extension file.
import<my_module>
import numpy
Package- It is a simple dictionary with having a collection of modules and sub-package modules.
from my_package.abc import a
8. Difference Between Range and Xrange?
Range()
It returns a list of integer
since range() returns a list of elements, it takes more memory
Execution speed is slower
It performs all kinds of arithmetic operations.
Xrange()
It returns a generator object.
It takes less memory
Execution speed is faster
arithmetic operation is not performed in Xrange()
9. Explain Ternary Operator in Python?
Ans- The syntax of python terenary statement
[if_true]if[expression]else[if_false]
10. What is Inheritance In Python
Ans- inheritance is the child class that acquires the properties it can access all
data members and their functions defined in the parent class.
Example- Class A(B)
11. What is Local Variable and Global Variable in Python
Ans- Local variable
It is declared inside the function
it is not initialized a garbage value which is stored
Data sharing is not allowed as data of the local variable can be accessed by only
one function
Global Variable
It is declared outside the function
It is not initialized and Zero is stored as the default
It is generated before the program global execution starts and
lost when the program terminates
Data sharing is allowed as multiple functions can access the same global
variable
12. Explain Break, Continue, and Pass Statement
Ans- Break statement will be used inside the loop will terminate the loop and exit.
for i in range(10):
if i==6:
break
print(i,end=",")
output
0,1,2,3,4,5
A Continue statement will stop the current execution when used inside the loop.
for i in range(10):
if i==6:
break
print(i,end=",")
output
0,1,2,3,4,5,6,7
A Pass statement is a null statement when the python interpreter comes across the pass statement. It does nothing and is ignored.
def my_func():
print('pass inside function')
pass
my_func()
output:
pass inside function
13. What is the 'self' keyword in python?
Ans- The "self" parameter is a reference to the current instant of the class and is used to access the variable which belongs to that class.
14. Explain Function of List, Set, Tuple, or Dictionary?
Function of Dictionary
clear(): Remove all elements from Dictionary
copy(): Returns a copy of the Dictionary
get(): Returns the value of the specified key
items(): Returns a list containing a tuple
keys(): Return list containing the dictionary keys
Function of set
add(): Adds an element to the list
clear(): Removes all the elements from the list
copy(): Returns a copy of the list
difference(): Return a set between two or more sets
discard(): Remove the specified items
Function of List
sort(): sort the list in ascending order
append(): Adds a single element in list
extend(): Adds a multiple elements to a list
max(): it returns max value
min(): it returns min value
Function of Tuple
cmp(tuple1,tuple2)-compare elements of both tuple
len(): total length of tuple
max(): Returns max value
min(): Returns min value
sum(): Returns sums value in tuple
15. What are Python Iterators?
Ans-An iterator is an object that contains a countable number of values. It is used
to convert other objects to an iterator using the iter() function.
Iterator uses iter() and next() function.
Iterator is not the generator
example:
iter_list=iter(['A','B','C'])
print(next(iter_list))
print(next(iter_list))
print(next(iter_list))
Output:
A
B
C