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 multicore 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 multicore 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
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
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
for i,char in enumerate(my_list):
The output is:
43. How does Python’s list.sort work at a high level? Is it stable? What’s the runtime?
In early pythonversions, 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.
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 **:
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 humanreadable, while repr() generates representations which can be read by the interpreter.
repr() returns a machinereadable representation of values, suitable for an exec command.
47. Python How do you make a higher order function in Python?
A higherorder 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:
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?
50. Are Tuples immutable?