Professional Documents
Culture Documents
15 Python Interview Questions - Advance
15 Python Interview Questions - Advance
Example:
2 – What are the principal differences between the lambda and def?
3 – Write a Regex pattern that confirms a .com email id using the python reg
expression module "re"?
r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com)$"
Python decorator gives us the ability to add new behavior to the given objects dynamically.
In the example below, we’ve written a simple example to display a message pre and post
the execution of a function.
www.unwiredlearning.com
For example, the below code will return all the numbers from 10 to 20 and store them in a
list.
6 – How do you debug a program in Python? Is it possible to step through the Python
code?
Yes, we can use the Python debugger (pdb) to debug any Python program. And if we start a
program using pdb, then it let us even step through the code.
mapreturns a map object (an iterator) which can iterate over returned values from applying a
function to every element in a sequence. The map object can also be converted to a list if
required.
def add_three(x):
return x + 3
li = [1,2,3]
print(list(map(add_three, li)))
#=> [4, 5, 6]
This can be tricky to wrap your head around until you use it a few times.
reduce takes a function and a sequence and iterates over that sequence. On each iteration,
both the current element and output from the previous element are passed to the function.
In the end, a single value is returned.
def add_three(x,y):
return x + y
li = [1,2,3,5]
reduce(add_three, li)
#=> 11
www.unwiredlearning.com
9 – Explain how the filter function works?
Filter literally does what the name says. It filters elements in a sequence.
Each element is passed to a function which is returned in the outputted sequence if the
function returns True and discarded if the function returns False.
def add_three(x):
if x % 2 == 0:
return True
else:
return False
li = [1,2,3,4,5,6,7,8]
[i for i in filter(add_three, li)]
#=> [2, 4, 6, 8]
Dict is python datatype, a collection of indexed but unordered keys and values.
JSON is just a string which follows a specified format and is intended for transferring data.
a = [1,2,3,4,5]
a2 = []
for i in a:
a2.append(i + 1)
print(a2)
#=> [2, 3, 4, 5, 6]
Solution:
#=> [2, 3, 4, 5, 6]
List comprehension is generally accepted as more pythonic where it’s still readable.
www.unwiredlearning.com
12 - What is pip and when is it used?
pip is a package management system and it is used to install many python packages. Eg.
Django, mysql.connector
Syntax:
13 - What is GIL? What does it do? 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.
Modules are independent Python scripts with the .py extension that can be reused in other
Python codes or scripts using the import statement. A module can consist of functions,
classes, and variables, or some runnable code. Modules not only help in keeping Python
codes organized but also in making codes less complex and more efficient. The syntax to
import modules in Python is as follows:
Whitespace represents the characters that we use for spacing and separation.
www.unwiredlearning.com