Posts

Showing posts from 2017

What are the different data types in Python?

Python Datatypes : Boolean Numbers Strings Lists Tuples Sets Dictionaries Boolean : Booleans are either true or false. Python has 2 constants named True and False which can be used to assign a boolean value directly. In certain places ( like if statements ), Python expects an expression to a boolean value, these places are called boolean contexts. Numbers : Python supports both int and floating point numbers. Tuples : A tuple is an immutable list, it means a tuple can't be changed in a way once it's created. A tuple is defined in the same way as a list, except that the whole set of an element is enclosed in the parenthesis instead of square brackets. Similarities with the list  : Tuples have a defined order i.e. Indices are 0 based, so the first element of a non-empty tuple is always a_tuple[ 0 ]. Negative indices count from the end of the tuple. Slicing works too, when we slice a list we get a new list. When we slice a tuple, we get a new t

What is composite key?

Composite Key : A composite key is a combination of two or more columns in a table that can be used to uniquely identify each row in the table. When the columns are combined uniqueness is guaranteed but when it taken individually it does not guarantee uniqueness. Sometimes more than one attributes are needed to uniquely identify an entity. A primary key that is made by the combination of more than one attribute is known as a composite key. Ex : Create table abc ( c1 int , c2 varchar(20), c3 varchar(100) , PRIMARY KEY ( c1, c2 ) );

Explain pickling and unpickling in python.

Pickling and Unpickling : Pickle is a standard module which serializes and deserializes a python object structure. Pickle module accepts any python object, converts it into a string representation and dumps into a file(by using dump() function ) which can be used later this process is called pickling. Unpickling is a process of retrieving original python object from the stored string representation for use. Pickling is a process whereby a python object hierarchy is converted into a byte stream and unpickling is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Ex: import pickle a = [ "apple" , "banana" ] file_Name = "abc" # open the file for writing fileObj = open( file_Name , "wb" ) # This writes the object a to the file pickle.dump( a , fileObj) fileObj.close() fileObj = open( file_Name , "r") # load the object from the file into variable b = pickle.load( fileO

What is list comprehension? Also explain generator comprehension, set comprehension and dictionary comprehension?

List Comprehension : List comprehension was introduced in 2.0. List comprehension is an elegant way to define and create a list in Python. List comprehension is a complete substitute for the lambda function as well as the functions map(), filter() and reduce(). >>> Celsius = [39.2, 36.5, 37.3, 37.8] >>> Fahrenheit = [ ((float(9)/5)*x + 32) for x in Celsius ] >>> print Fahrenheit [102.56, 97.700000000000003, 99.140000000000001, 100.03999999999999] Generator Comprehension : Generator comprehensions were introduced with Python 2.6.  They are simply a generator expression with a parenthesis - round brackets - around it. Otherwise, the syntax and the way of working is like list comprehension, but a generator comprehension returns a generator instead of a list. >>> x = (x **2 for x in range(20)) >>> print(x) at 0xb7307aa4> >>> x = list(x) >>> print(x) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 10

Explain slicing and indexing operations.

Slicing and Indexing operations : Indexing operation allows to access a particular item in the sequence directly. Slicing operation allows to retrieve a part of the sequence. In slicing : S[ StartNo : StopNo : Step ] If the start no is omitted, Python will start at the beginning of the sequence. If the stop no is omitted, Python will stop at the end of the sequence. >>> a = [1,2,3,4] >>> a[0:2] # take items 0-2, upper bound noninclusive [1, 2] >>> a[0:-1] #take all but the last [1, 2, 3] >>> a[1:4] [2, 3, 4] >>> a[::-1] # reverse the list [4, 3, 2, 1] >>> a[::2] # skip 2 [1, 3]

What is persistent cookie?

Persistent Cookie : Also called a permanent cookie, or a stored cookie, a cookie that is stored on a user's hard drive until it expires (persistent cookies are set with expiration dates) or until the user deletes the cookie.  Persistent cookies are used to collect identifying information about the user, such as Web surfing behavior or user preferences for a specific Web site. A persistent cookie enables a website to remember you on subsequent visits, speeding up or enhancing your experience of services or functions offered. For example, a website may offer its contents in different languages. On my first visit, I may choose to have the content delivered in French and the site may record that preference in a persistent cookie set on your browser. When you revisit that site it will use the cookie to ensure that the content is delivered in French. Session Cookie : Also called a transient cookie, a cookie that is erased when the user closes the Web browser. The session c

Interview questions and answers

Ques: How are the functions help() and dir() different? Ans: These are the two functions that are accessible from the Python Interpreter. These two functions are used for viewing a consolidated dump of built-in functions. help : It will display the documentation string. It is used to see the help related to modules, keywords, attributes, etc. To view the help for a keyword, topics, you just need to type, help("<keyword>") dir : Without arguments, it will return list of names in the current local scope.  With an argument, it will return a list of valid attributes for that object. Ques:  Which statement of Python is used whenever a statement is required syntactically but the program needs no action? Ans: p ass is no-operation / action statement in Python. If we want to get data from a model  and even if the requested data does not exist, we want to continue with other tasks. In such a scenario, use try-except block with pass statement in the exc

How sessions work in Django?

The session framework stores and retrieves data. It stores the data on the server side and sends and receives the cookies. Cookies contain a session ID, not the data itself unless we are using the cookie-based sessions. Django provides support for anonymous sessions. Configuring the session engine: By default, Django stores sessions in our database. We can configure Django to store session data using these sessions engines: Database-backed sessions Cached sessions File-based sessions Cookie-based sessions Database-backed sessions: To use databased-backed sessions we need to add "django.contrib.sessions" to our INSTALLED_APPS setting. After this configuration, we need to run manage.py migrate to install the database table that stores session data. Cached sessions: For better performance, we can use a cached-based session backend. We should only use it if we are using Memcached cache backend. File-based sessions: To use file-based sess

What is middleware in django?

Image
Middleware  : Middleware is a framework of hooks into Django’s request/response processing. Middleware is a lightweight plugin for Django. It allows modifying request or response or the view parameters. Each middleware component is responsible for doing some specific function. For example, Django includes a middleware component, AuthenticationMiddleware, that associates users with requests using sessions. When to use middleware  : We can use middleware if you want to modify the request i.e HttpRequest object which is sent to the view. Or you might want to modify the HttpResponse object returned from the view. Both these can be achieved by using middleware. We might want to perform an operation before the view executes. In such case, we would use a middleware. Hooks and Application order  : Request phase  : During the request phase, before calling the view. Django applies middleware in the order it's defined in MIDDLE_CLASSES top-down. 2 hooks are available : proc

What is difference between multi-threading and multiprocessing?

Image
Multi-threading vs Multiprocessing : The threading module uses threads, the multiprocessing uses processes. The difference is that threads run in the same memory space, while processes have separate memory.This makes it a bit harder to share objects between processes with multiprocessing. Since threads use the same memory, precautions have to be taken or two threads will write to the same memory at the same time. This is what the global interpreter lock is for. Multiprocessing : Pross : Separate memory space Takes advantage of multiple CPUs & cores Avoids GIL limitations for cPython Child processes are interruptible/killable Cons : Inter-process communication (IPC) a little more complicated with more overhead. Larger memory footprint. Multi-threading : Pross : Lightweight - low memory footprint. Shared memory - makes access to state from another context easier. Allows you to easily make responsive UIs. cPython C extension modules that properly releas

How are variables and memory managed in Python?

Image
Memory management in Python : When we create an object then the Python Virtual Machine handles the memory needed and where it shall be placed in the memory layout. CPython uses a private heap for storing objects. 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. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching. The algorithm used for garbage collecting is called Reference counting. That is the Python VM keeps an internal journal of how many references refer to an object, and automatically garbage collects it when there are no more references referring to it. The management of the Python heap is performed by the interpreter itself and that the user has no control over it. At the lowest level Raw Memory Allocator interacting w

Explain about assert statement?

Assertion : An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the program. We often place assertions at the start of a function to check for valid input, and after a function call to check for valid output. The assert Statement  : When encounters an assert statement, Python evaluates the accompanying expression, which is hopefully true. If the expression is false, Python raises an AssertionError exception. Syntax : assert Expression[, Arguments] If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback. Ex : >>> def addOne(n): ...     assert (n >= 0),"n should be greator than or equal to 0." ...     return n + 1 ...  >>> addOne(1) 2 >>> addOne

What is a negative index?

Negative index in python : Python list items can be accessed with positive or negative index.  If list of size n then  0 positive index // first index 1 positive index // second index (n-1) // last index -n // first index -(n-1) // second -1 // last index List indexes of -x mean the xth item from the end of the list, so n[-1] means the last item in the list n. Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on. Ex:  If we have a list a = [1,2,3,4] then a[-1] will refer to 4 and a[-2] refer to 3.

What is __repr__ function?

__repr__ function : The __repr__ function returns a string representation of a Python object that may be evaluated by the Python interpreter to instantiate another instance of the object. Called by the repr() built-in function and by string conversions (reverse quotes) to compute the "official" string representation of an object. repr() tries to convert everything into a string that can be used with eval() to re-generate the original object. repr() generates a computer friendly output. str() is used by print() function to generate a user-friendly output. str()'s output may or may not be used with eval(). >>> eval("'Hello'") 'Hello' >>> eval('Hello') Traceback (most recent call last):   File "<pyshell#8>", line 1, in <module>     eval(' Hello ')   File "<string>", line 1, in <module> NameError: name ' Hello ' is not defined

What is Python?

Python : Python is an interpreted language. Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby. Python is dynamically typed, this means that you don't need to state the types of variables when you declare them or anything like that. You can do things like x=123 and then x="Hello" without an error. Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects. Writing Python code is quick but running it is often slower than compiled languages. Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it's really quite quick because a lot of the number crunching

Describe functions in Python

Python Functions : A function is a block of organized, reusable code that is used to perform a single, related action. Python gives us many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions. Parameter Passing : We can pass arguments to a function either by value or by reference. Call by Value : Sometimes it's also called pass by value. In call-by-value, the argument expression is evaluated, and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local copy of its value will be used, i.e. the variable in the caller's scope will be unchanged when the function returns. Call by Reference : It is also known as pass-by-reference. In this a function gets an implicit reference to the argument, rather than a copy of its value. As a consequence, the function can modify the argument, i.e. the value of the variable in

What are Lambda expressions in python?

Lambda expressions : Lambda functions are single-expression functions that are not necessarily bound to a name. Lambda expression is an anonymous function. Executing a lambda function evaluates its expression and then automatically returns its result. So there is always an implicit return statement. It's a shorthand that allows us to write a method in the same place we are going to use it. The difference between lambdas and regular function definitions is that lambda functions are restricted to a single expression. This means a lambda function can't use statements or annotations, not even a return statement. Example: double = lambda x: x * 2 double(2) # it will return 4

Explain decorators in python

Decorator : Decorator is the name used for a software design pattern. It is used to alter functions and methods. Decorators work as wrappers, modifying the behavior of the code before and after a target function execution, without the need to modify the function itself, augmenting the original functionality. Basically, a decorator takes in a function, adds some functionality and returns it. Multiple decorators can be chained in Python. For ex : def addOne(myFun): def addOneInside(*args,**kwrgs): return myFun(*args,**kwrgs) + 1 return addOneInside @addOne def oldFun( x = 1 ): return x print oldFun(564) # prints 565

Save a file in model without using Django forms

If you want to store the document without using Django forms then you can use below code :  in models.py ... class UserProfile(models.Model) :     user = models.OneToOneField(User)     doc = models.FileField(upload_to='uploads/', blank=True) in python script from core.models import UserProfile from django.core.files import File user1 = UserProfile(pk = 1) user1.doc.save('resume.pdf', File(open('/tmp/resume.pdf', 'r')))

Explain Inheritance in python.

Inheritance : Inheritance enables us to define a class that takes all the functionality from a parent class and allows us to add more. Classes can inherit from other classes. A class can inherit attributes and behavior methods from another class, called the superclass. A class which inherits from a superclass is called a subclass, also called heir class or child class. Superclasses are sometimes called ancestors as well. Syntax : class DerivedClass(BaseClass) :    body_of_derived_class A derived class inherits features from the base class, adding new features to it. This results in re-usability of code. If an attribute is not found in the class, the search continues to the base class. This repeats recursively if the base class is derived from other classes. Ex : class Polygon:    def __init__(self, no_of_sides):        self.n = no_of_sides        self.sides = [0 for i in range(no_of_sides)]    def inputSides(self):        self.sides =

What are Iterators and Generators in python?

Iterators : An iterator is an object which allows us to traverse through all the elements of a collection, regardless of its specific implementation. An iterator is an object which implements the iterator protocol. The iterator protocol consists of two methods : The __iter__() method, which must return the iterator object The next() method, which returns the next element from a sequence Python has several built-in objects, which implement the iterator protocol. For example lists, tuples, strings, dictionaries or files. Advantages : Cleaner code Iterators can work with infinite sequences Iterators save resources By saving system resources means that when working with iterators, we can get the next element in a sequence without keeping the entire dataset in memory. Generators : Generators functions allow us to declare a function that behaves like an iterator. A generator yields one value at a time, this requires less memory. Gene