Python Interview Questions and Answers Set 5

41. What is GIL? What does it do?Talk to me about the GIL. How does it impact concurrency in Python? What kinds of applications does it impact more than others?

Python’s GIL is intended to serialize access to interpreter internals from different threads. On multi­core systems, it means that multiple threads can’t effectively make use of multiple cores. (If the GIL didn’t lead to this problem, most people wouldn’t care about the GIL ­ it’s only being raised as an issue because of the increasing prevalence of multi­core systems.)

Note that Python’s GIL is only really an issue for CPython, the reference implementation. Jython and IronPython don’t have a GIL. As a Python developer, you don’t generally come across the GIL unless you’re writing a C extension. C extension writers need to release the GIL when their extensions do blocking I/O, so that other threads in the Python process get a chance to run.

Print the index of a specific item in a list? Ans.use the index() function

>>>[“foo”,”bar”,”baz”].index(‘bar’)

42. How do you iterate over a list and pull element indices at the same time?

You are looking for the enumerate function. It takes each element in a sequence (like a list) and sticks it’s location right before it. For example

>>>my_list =[‘a’,’b’,’c’]

>>>list(enumerate(my_list))

[(0,’a’),(1,’b’),(2,’c’)]

Note that enumerate() returns an object to be iterated over, so wrapping it in list() just helps us see what enumerate() produces.

An example that directly answers the question is given below

my_list =[‘a’,’b’,’c’]

for i,char in enumerate(my_list):

print i,char

The output is:

0 a

1 b

2 c

43. How does Python’s list.sort work at a high level? Is it stable? What’s the runtime?

In early python­versions, the sort function implemented a modified version of quicksort. However, it was deemed unstable and as of 2.3 they switched to using an adaptive mergesort algorithm.

44. What does the list comprehension do:

my_list=[(x,y,z) for x in range(1,30) for y inrange(x,30) for z in range(y,30) if x**2+y**2==z**2]

It creates a list of tuples called my_list, where the first 2 elements are the perpendicular sides of right angle triangle and the third value ‘z’ is the hypotenuse.

[(3,4,5),(5,12,13),(6,8,10),(7,24,25),(8,15,17),(9,12,15), (10,24,26),(12,16,20),(15,20,25),(20,21,29)]

45. How can we pass optional or keyword parameters from one function to another in Python?

Gather the arguments using the * and ** specifiers in the function’s parameter list. This gives us positional arguments as a tuple and the keyword arguments as a dictionary. Then we can pass these arguments while calling another function by using * and **:

deffun1(a,*tup,**keywordArg):

keywordArg[‘width’]=’23.3c’

Fun2(a,*tup,**keywordArg)



46. Explain the role of repr function.

Python can convert any value to a string by making use of two functions repr() or str(). The str() function returns representations of values which are human­readable, while repr() generates representations which can be read by the interpreter.

repr() returns a machine­readable representation of values, suitable for an exec command.

47. Python ­ How do you make a higher order function in Python?

A higher­order function accepts one or more functions as input and returns a new function. Sometimes it is required to use function as data To make high order function , we need to import functools module The functools.partial() function is used often for high order function.

48. What is map?

The syntax of map is:

map(a.Function,a.Sequence)

The first argument is a function to be executed for all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given.

49. Tell me a very simple solution to print every other element of this list?

=[0,10,20,30,40,50,60,70,80,90]

Lhttps://www.linkedin.com/redir/invalid-link-page?url=%5B%3A%3A2%5D

50. Are Tuples immutable?

Yes.