What are different data types in python – LSD
mutable – Sandeep Kanao
What
are immutable and non-immutable objects – Sandeep Kanao
a mutable object can be changed after it is created, and
an immutable object
can't
How make class immutable – Sandeep Kanao
Option 1 :
The easiest way to do this is using
__slots__
class A(object):
__slots__ = []
Option 2 :
completely disallow
__setattr__
and
use object.__setattr__
in the constructor
class Point(object):
def __init__(self, x, y):
object.__setattr__(self, "x", x)
object.__setattr__(self, "y", y)
def __setattr__(self, *args):
raise TypeError
what is difference between __new__ and
__init__ – Sandeep Kanao
__new__ is the first step
of instance creation. It's called first, and is responsible for returning a new
instance of your class. In contrast,__init__ doesn't
return anything; it's only responsible for initializing the instance after it's
been created.
In
general, you shouldn't need to override __new__ unless
you're subclassing an immutable type like str, int, unicode or tuple.
How to find duplicates in List –
Sandeep Kanao
>>> l = [1,2,3,4,4,5,5,6,1]
>>> set([x for x in l if l.count(x) > 1])
set([1, 4, 5])
How to Compare to two lists and get the
differences – Sandeep Kanao
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
Option 1 :
list(set(temp1) - set(temp2)) -- Use set
Option 2 :
s = set(temp2)
temp3 = [x for x in temp1 if x not in s]
Option 3:
Use python
library
pip install deepdiff
Option 4 :
Use NumPy
difference
What is Decorator – Sandeep Kanao
Decorators
are functions which modify the functionality of another function. They help to
make our code shorter
Python
allows you to simplify the calling of decorators using the @ symbol (this is
called “pie” syntax).
Decorators
are callable objects which are used to modify functions or classes.
Callable
objects are objects which accepts some arguments and returns some objects.
functions and classes are examples of callable objects in Python.
Function
decorators are functions which accepts function references as arguments and
adds a wrapper around them and returns the function with the wrapper as a new
function
classes
can also be used to build decorators (example - log, emaillog(log)- change
decorator from @long to @emaillog - to get extend log)
Everything
in Python is an object
1. Nested
functions - Defining functions within functions
def
hi(name="sandeep"):
print("inside the hi() function")
def greet():
return "inside the greet()
function"
def welcome():
return "inside the welcome()
function"
>>hi()
output :
inside the
hi() function
inside the
greet() function
inside the
welcome() function
2.
Returning functions from within functions
def
hi(name="sandeep"):
def greet():
return "inside the greet()
function"
def welcome():
return "inside the welcome() function"
if name == "sandeep":
return greet
else:
return welcome
>>a
= hi()
>>print(a())
inside the
greet() function
3. Giving
a function as an argument to another function
def hi():
return "hi sandeep!"
def
doSomethingBeforeHi(func):
print("inside
doSomethingBeforeHi")
print(func())
doSomethingBeforeHi(hi)
#outputs:inside
doSomethingBeforeHi
# hi sandeep!
Decorators
:
def
decorator_func(some_func):
# define another wrapper function which
modifies some_func
def wrapper_func():
print("Wrapper function started")
some_func()
print("Wrapper function ended")
return wrapper_func # Wrapper function add
something to the passed function and decorator returns the wrapper function
def
say_hello():
print ("Hello")
say_hello
= decorator_func(say_hello)
say_hello()
# Output:
# Wrapper function started
# Hello
# Wrapper function started
Syntax
@decorator_func
def
say_hell():
print 'Hello'
def
say_hello():
print 'Hello'
say_hello
= deocrator_func(say_hello)
Functions
with arguments and decorators, inner function must have same number of
arguments
Where are decorators used -use case –
Sandeep Kanao
1.
Authorization - Flask web framework: Decorators can help to check whether
someone is authorized to use an endpoint in a web application (@wraps)
2. Logging
3. Flask
routing (@app.route(‘/hello’))
What are Generators
There is a lot of overhead in building an iterator in Python; we have to implement a class
with
__iter__()
and __next__()
method,
keep track of internal states, raise StopIteration
when
there was no values to be returned etc.
If a function contains at least one
yield
statement
(it may contain other yield
or return
statements),
it becomes a generator function. Both yield
and return
will
return some value from a function.
# A simple generator function
def my_gen():
n = 1
print('This is
printed first')
# Generator
function contains yield statements
yield n
n += 1
print('This is
printed second')
yield n
n += 1
print('This is
printed at last')
yield n
# Using for loop
for item in my_gen():
print(item)
output :
This is printed first
1
This is printed second
2
This is printed at last
3
Generator expressions – Sandeep Kanao
The syntax for generator
expression is similar to that of a list
comprehension in Python. But the square brackets are replaced with
round parentheses.
The major difference between a
list comprehension and a generator expression is that while list comprehension
produces the entire list, generator expression produces one item at a time.
# Intialize the list
my_list = [1, 3, 6, 10]
a = (x**2 for x in my_list)
# Output: 1
print(next(a))
# Output: 9
print(next(a))
Why generators are used in Python –
Sandeep Kanao
1. Easy to Implement
2. Memory Efficient
3. Represent Infinite Stream
4. Pipelining Generators
Differences between Generator function
and a Normal function – Sandeep Kanao
·
Generator
function contains one or more yield statement.
·
When
called, it returns an object (iterator) but does not start execution
immediately.
·
Methods
like __iter__() and __next__() are implemented automatically. So we can iterate
through the items using next().
·
Once
the function yields, the function is paused and the control is transferred to
the caller.
·
Local
variables and their states are remembered between successive calls.
·
Finally,
when the function terminates, StopIteration is raised automatically on further
calls.
What is
Map – Sandeep Kanao
map functions
expects a function object and any number of iterables like list, dictionary,
etc. It executes the function_object for each element in the sequence and
returns a list of the elements modified by the function object.
map(function_object,
iterable1, iterable2,...)
def
multiply2(x):
return x * 2
map(multiply2,
[1, 2, 3, 4]) # Output [2, 4, 6, 8]
example –
adding elements in two lists
list_a =
[1, 2, 3]
list_b =
[10, 20, 30]
map(lambda
x, y: x + y, list_a, list_b) # Output: [11, 22, 33]
What is
Filter – Sandeep Kanao
filter function
expects two arguments, function_object and an iterable. function_object returns
a boolean value
filter(function_object,
iterable)
Unlike map function filter function can
only have one iterable as input.
Example
a = [1, 2,
3, 4, 5, 6]
filter(lambda
x : x % 2 == 0, a) # Output: [2, 4, 6]
What is Reduce
reduce(fun,seq)
functools.
reduce(lambda
a,b : a
if
a > b
else
b,lis)
functools.
reduce(lambda
a,b : a
+b,lis)
What are different collection types in
Python – Sandeep Kanao
What is tuple and how it is different
than list – Sandeep Kanao
List
|
Tuple
|
mutable
|
immutable -
Cannot change the values in a tuple once created
|
[]
|
{}
|
variable length
|
fixed length
|
lists show order
|
Tuples show structure
|
Better performance
|
What is difference between list and
dictionary
List
|
Dictionary
|
mutable
|
mutable
|
maintain their ordering
|
Ordering
is not guaranteed
|
can be
of any type, and types can be mixed
|
Key
values can be of any hashtable type (i.e. not a dict) and types can be mixed
Values
can be of any type (including other dict’s), and types can be mixed
|
accessed
via numeric (zero based) indices
|
accessed using key values
|
use a
list if you have an ordered collection of items.
|
Use a dictionary when you have a set of unique keys that map to values
|
What is
@StaticMethod and @ClassMethod – Sandeep
Kanao
To define a class method in python, we use @classmethod
decorator and to define a static method we use @staticmethod decorator.
Let us look at an example to understand the difference between both of them. Let us say we want to create a class Person. Now, python doesn’t support method overloading like C++ or Java so we use class methods to create factory methods.
Let us look at an example to understand the difference between both of them. Let us say we want to create a class Person. Now, python doesn’t support method overloading like C++ or Java so we use class methods to create factory methods.
# use of class method and static method.
from datetime import date
class Person:
def __init__(self, name,
age):
self.name
= name
self.age
= age
# a class method to
create a Person object by birth year.
@classmethod
def fromBirthYear(cls,
name, year):
return
cls(name, date.today().year - year)
# a static method to
check if a Person is adult or not.
@staticmethod
def isAdult(age):
return
age > 18
person1 = Person('mayank', 21)
person2 = Person.fromBirthYear('mayank', 1996)
print person1.age
print person2.age
# print the result
print Person.isAdult(22)
output
21
21
True
When
to use what?
§ We generally use class method to create factory methods. Factory
methods return class object ( similar to a constructor ) for different use
cases.
§ We generally use static methods to create utility functions.
What is
if __name__ == "__main__" – Sandeep Kanao
When you
execute a Python script , it is treated as the main and its __name__ attribute
is set to "__main__" . If you import this script as a module in
another script, the __name__ is set to the name of the script/module.
By doing
the main check, you can have that code only execute when you want to run the
module as a program and not have it execute when someone just wants to import
your module
How
Python handles multiple inheritance (Diamond problem in Python) - Method
Resolution Order (MRO) – Sandeep Kanao
Python 2.x
depth-first and then left-to-right
What is GIL – Sandeep Kanao
A global
interpreter lock (GIL) is a mechanism used in computer-language interpreters to
synchronize the execution of threads so that only one native thread can execute
at a time.[1] An interpreter that uses GIL always allows exactly one thread to
execute at a time, even if run on a multi-core processor.
What is __REPR__
Dunder or
magic methods in Python are the methods having two prefix and suffix
underscores in the method name. Dunder here means “Double Under (Underscores)”.
These are commonly used for operator overloading. Few examples for magic
methods are: __init__, __add__, __len__, __repr__ etc.
How to
include python module (~/dirone/1.py) to another python module (~/dirtwo/2.py)
1. from ../dir1 import 1.py
2. Update
PYTHONPATH env variable
3. import sys
sys.path.insert(1, '.')
Explain Docstring
Python
documentation strings (or docstrings) provide a convenient way of associating
documentation with Python modules, functions, classes, and methods.
def sum(x,
y):
"""Returns arg1 value add to
arg2 value."""
return a+b
print
sum.__doc__
output
Returns
arg1 value add to arg2 value.
What is
difference between Global and Return – Sandeep Kanao
Global variable means that we can access that variable
outside the function as well
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
from collections import namedtuple
Point = namedtuple('Point', 'x y')
pt1 = Point(1.0, 5.0)
pt2 = Point(2.5, 1.5)
from math import sqrt
line_length = sqrt((pt1.x-pt2.x)**2 + (pt1.y-pt2.y)**2)
Explain file
manipulation and 'with' – Sandeep Kanao
with open('output.txt', 'w') as f:
f.write('Hi
there!')
The above with statement will automatically close the
file after the nested block of code, even if exception occurs
What is the difference between deep
and shallow copy? – Sandeep Kanao
In case of
deep copy, a copy of object is copied in other object. It means that any
changes made to a copy of object do not reflect in the original object.
In python,
this is implemented using “deepcopy()” function.
#
importing "copy" for copy operations
import
copy
#
initializing list 1
li1 = [1,
2, [3,5], 4]
# using
deepcopy to deep copy
li2 =
copy.deepcopy(li1)
Shallow
copy
In case of
shallow copy, a reference of object is copied in other object. It means that
any changes made to a copy of object do reflect in the original object.
In python,
this is implemented using “copy()” function
# importing "copy" for copy operations
import copy
# initializing list 1
li1 = [1, 2, [3,5], 4]
# using copy to shallow copy
li2 = copy.copy(li1)
Find by value in dicionary
mydict =
{'george':16,'amber':19}
option 1 ;
[name for
name, age in mydict.items() if age == search_age]
option2 ;
p =
dict(zip(mydict.values(),mydict.keys()))
option 3:
import
pandas as pd
mydict =
{'george':16,'amber':19}
mydict_lookup
= pd.Series(mydict)
lookup_list[mydict_lookup.values
== 19]
option 4 :
lookup =
{value: key for key, value in mydict }
lookup[value]
How is memory managed in Python? –
Sandeep Kanao
Memory
management in Python involves a private heap containing all Python objects and
data structures. The management of this private heap is ensured internally by
the Python memory manager. At the lowest level, a raw memory allocator ensures
that there is enough room in the private heap for storing all Python-related
data by interacting with the memory manager of the operating system.
What is the usage of help() and dir()
function in Python? – Sandeep Kanao
What is monkey patching in Python? –
Sandeep Kanao
Monkey patching is changing the behavior of a function or
object after it has already been defined. For example:
import datetime
datetime.datetime.now
= lambda: datetime.datetime(2012, 12, 12)
Used for
testing
Write a one-liner that will count the
number of capital letters in a file. Your code should work even if the file is
too big to fit in memory.
count sum(1
for
line in
fh for
character in
line if
character.isupper())
Write a sorting algorithm for a
numerical dataset in Python. – Sandeep Kanao
list = ["1", "4",
"0", "6", "9"]
list = [int(i) for i in list]
list.sort()
print (list)
What is pickling and unpickling? –
Sandeep Kanao
It is used for serializing and de-serializing a Python object
structure. Any object in python can be pickled so that it can be saved on disk
(dump)
How to get indices of N maximum values
in a NumPy array?
Explain zip – Sandeep Kanao
zip takes n number of iterables and returns list of
tuples
list_a =
[1, 2, 3, 4, 5]
list_b =
['a', 'b', 'c', 'd', 'e']
zipped_list
= zip(list_a, list_b)
print
zipped_list # [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
If the
length of the iterables are not equal, zip creates the list of tuples of length
equal to the smallest iterable.
zip always creates the tuple in the order of iterbales
from left to right
To unzip a list of tuples we zip(*listP_of_tuples).
Unzip creates separate list
zipper_list
= [(1, 'a'), (2, 'b'), (3, 'c')]
list_a, list_b = zip(*zipper_list)
print list_a # (1, 2, 3)
print list_b # ('a', 'b', 'c')
Pass – is no-operation / action
statement in Python-statement of Python is used whenever a statement is
required syntactically but the program needs no action
find vovels in string – Sandeep Kanao
final
=
[each
for
each
in
string
if
each
in
vowels]
How to create an Enum
in Python?
– Sandeep Kanao
from
enum import Enum
class
Directions(Enum):
East, West,North,South = range(4)
print(Directions.North.value)
##output 2
What are the drawbacks of Python? –
Sandeep Kanao
Disadvantages
of Python are:
Speed
Python is
slower than C or C++. But of course, Python is a high-level language, unlike C
or C++ it's not closer to hardware.
Mobile
Development
Python is
not a very good language for mobile development . It is seen as a weak language
for mobile computing. This is the reason very few mobile applications are built
in it like Carbonnelle.
Memory
Consumption
Python is
not a good choice for memory intensive tasks. Due to the flexibility of the
data-types, Python's memory consumption is also high.
Database
Access
Python has
limitations with database access . As compared to the popular technologies like
JDBC and ODBC, the Python's database access layer is found to be bit
underdeveloped and primitive . However, it cannot be applied in the enterprises
that need smooth interaction of complex legacy data .
Runtime
Errors
Python
programmers cited several issues with the design of the language. Because the language
is dynamically typed , it requires more testing and has errors that only show
up at runtime .