Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

15 Python Interview Questions - Advance

1 – What is a lambda function?


An anonymous function is known as a lambda function. This function can have any number
of parameters but can have just one statement.

Example:

sum = ​lambda​ x,y : x+y


print(sum(​10​, ​20​))

2 – What are the principal differences between the lambda and def?

● Def can hold multiple expressions while lambda is a uni-expression function.


● Def generates a function and designates a name to call it later. Lambda forms a
function object and returns it.
● Def can have a return statement. Lambda can’t have return statements.
● Lambda supports to get used inside a list and dictionary.

3 – Write a Regex pattern that confirms a .com email id using the python reg
expression module "re"?

Python has a regular expression module “re.”

r"[0-9a-zA-Z.]+@[a-zA-Z]+\.(com)$"

4 – What are Decorators in Python?

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.

5 – What is the syntax for List comprehension in Python?

The signature for the list comprehension is as follows:

[ expression(var) ​for​ var ​in​ iterable ]

www.unwiredlearning.com 
For example, the below code will return all the numbers from 10 to 20 and store them in a
list.

alist = [var ​for​ var ​in​ range(​10​, ​20​)]


print(alist)

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.

7 – Explain how the map function works?

map​returns 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]

8 – Explain how the reduce function works?

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.

from​ functools ​import​ reduce

def​ add_three(x,y):
​return​ x + y
li = [​1​,​2​,​3​,​5​]
reduce(add_three, li)
#=> 11

11​ is returned which is the sum of ​1+2+3+5​.

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]

Note how all elements not divisible by 2 have been removed.

10 – What is the difference between dictionaries and JSON?

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.

11 – Convert the following for loop into a list comprehension.

a = [​1​,​2​,​3​,​4​,​5​]

a2 = []
for​ i ​in​ a:
a2.append(i + ​1​)
print(a2)
#=> [2, 3, 4, 5, 6]

Solution:

a3 = [i+​1​ ​for​ i ​in​ a]


print(a3)

#=> [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:

pip install package_name

pip install Django

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 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.

14 - What is a Python module?

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:

import​ module_name ​# include this code line on top of the script

15 - What is whitespace in Python?

Whitespace represents the characters that we use for spacing and separation.

They possess an “empty” representation. In Python, it could be a tab or space.

www.unwiredlearning.com 

You might also like