Python Interview Questions For Experienced
Python Interview Questions For Experienced
What is PEP 8?
Pickle module accepts any Python object and converts it into a string
representation and dumps it into a file by using dump function, this process is
called pickling. While the process of retrieving original Python objects from the
stored string representation is called unpickling.
What are the tools that help to find bugs or perform static analysis?
PyChecker is a static analysis tool that detects the bugs in Python source
code and warns about the style and complexity of the bug. Pylint is another
tool that verifies whether the module meets the coding standard.
What are Python decorators?
A lambda form in python does not have statements as it is used to make new
function object and then return them at runtime.
In Python, iterators are used to iterate a group of elements, containers like list.
A mechanism to select a range of items from sequence types like list, tuple,
strings etc. is known as slicing.
In order to convert a number into a string, use the inbuilt function str(). If you
want a octal or hexadecimal representation, use the inbuilt function oct() or
hex().
Xrange returns the xrange object while range returns the list, and uses the
same memory and no matter what the range size is.
In Python, module is the way to structure program. Each Python program file
is a module, which imports other modules like objects and attributes.
Explain what is the common way for the Flask script to work?
Explain what is Dogpile effect? How can you prevent this effect?
Dogpile effect is referred to the event when cache expires, and websites are
hit by the multiple requests made by the client at the same time. This effect
can be prevented by using semaphore lock. In this system when value
expires, first process acquires the lock and starts generating new value.
import random
random.shuffle(list)
random.shuffle(list)
We can use Python <split()> function to break a string into substrings based on the
defined separator. It returns the list of all words present in the input string.
Program Output.
In Python, strings are just like lists. And it is easy to convert a string into the list. Simply
by passing the string as an argument to the list would result in a string-to-list conversion.
Program Output.
How does exception handling in Python differ from Java? Also, list the
optional clauses for a <try-except> block in Python?
Unlike Java, Python implements exception handling in a bit different way. It provides an
option of using a <try-except> block where the programmer can see the error details
without terminating the program. Sometimes, along with the problem, this <try-except>
statement offers a solution to deal with the error.
There are following clauses available in Python language.
1. try-except-finally
2. try-except-else
No objects in Python have any associated names. So there is no way of getting the one
for an object. The assignment is only the means of binding a name to the value. The
name then can only refer to access the value. The most we can do is to find the
reference name of the object.
items.append (1)
return items
print fast ()
print fast ()
[1]
[1, 1]
The function <fast> evaluates its arguments only once after the function gets defined.
However, since <items> is a list, so it’ll get modified by appending a <1> to it.
keyword = 'aeioubcdfg'
<'aeioubcdfg'>
In Python, while performing string slicing, whenever the indices of both the slices collide,
a <+> operator get applied to concatenates them.
How would you produce a list with unique elements from a list with
duplicate elements?
Ans. Iterating the list is not a desirable solution. The right answer should look like this.
duplicates =
['a','b','c','d','d','d','e','a','b','f','g','g','h']
uniqueItems = list(set(duplicates))
print sorted(uniqueItems)
Can you iterate over a list of words and use a dictionary to keep track
of the frequency(count) of each word? Consider the below example.
def dic(words):
wordList = {}
try:
wordList[index] += 1
except KeyError:
wordList[index] = 1
return wordList
wordList='1,3,2,4,5,3,2,1,4,3,2'.split(',')
print wordList
print dic(wordList)
['1', '3', '2', '4', '5', '3', '2', '1', '4', '3', '2']
class Test(object):
def __init__(self):
self.x = 1
t = Test()
print t.x
print t.x
print t.x
print t.x
Ans. All print statement will display <1>. It’s because the value of object’s attribute(x) is
never changing.
# Do something with n
print n