Python Interview Questions India
Python Interview Questions India
Will it give any error? If yes, which error and why? If not, then why?
I want another tuple t1 as ((1, “A”), (2, “A”), (3, “A”)). Give me all possible solutions. Which is much
better and why?
for num in a:
s += num
print s
57. I have a list “a” of objects of same type, where n is a property of that object which contains number. How to get
sum of all ‘n’ of objects? Give all possible solutions.
sum([m.n for m in a])
reduce(lambda x, y: x.n + y.n, a)
s= 0
for num in a:
s += num.n
print s
58. What is difference between List and Array?[List can have data of different type where as Array can have same
type of data]
59. How to copy objects in python? What is difference between Shallow Copy and Deep Copy?
60. How to make a variable global? What is difference between local and global variable? Some more questions on
local and global variable.
61. Have you used map function in python? Explain with example.
62. How to create dictionary from two lists? Ans: dict(zip(l1,l2))
63. What is named tuple?
namedtuple is a factory function for making a tuple class. With that class we can create tuples that are
callable by name also.
Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code.
They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of
position index.
You should use named tuples instead of tuples anywhere you think object notation will make your code
more pythonic and more easily readable.
You can also replace ordinary immutable classes that have no functions, only fields with them.
66. What is “Least Astonishment” in Python i.e The Mutable Default Argument? How to resolve it in pythonic
way?
JPMC or BOA Interview questions:
L = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9] ]
L = [ [1, 4, 7],
[2, 5, 8],
[2, 6, 9] ]
[Ans: zip(*L)]
I have a list say “L” which contains numbers from 1 to 100, but one element is missing. How to find the
missing element? Ans: sum(xrange(1,101)) – sum(L)
I have a list say “L” which contains n numbers or elements, only one element is duplicate. Remove that
duplicate element. Ans: list(set(L))
Some questions on decorators.
How memory management done in python? [Ans: http://foobarnbaz.com/2012/07/08/understanding-
python-variables/]
What all design patters you have used?(Singleton, decorator, façade, proxy etc)
How to implement Singleton in python?
Some questions on magic methods?[In Face to Face round] [Ans:
http://www.rafekettler.com/magicmethods.html]
Have you used Mixin in python?[JPMC] Ans: (Just google it. It is nothing but multiple inheritance.)
A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used: