Python Interview Questions and Answers - Python Interview Questions and answers are here to help you get your dream Job.
What is Python? What are the benefits of using Python?
Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open source.

What is PEP 8?
PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.

What is pickling and unpickling?
Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling.  While the process of retrieving original Python objects from the stored string representation is called unpickling.

How Python is interpreted?
Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed.

How memory is managed in Python?
  • Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter takes care of this Python private heap.
  • The allocation of Python heap space for Python objects is done by Python memory manager.  The core API gives access to some tools for the programmer to code.
  • Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.
What are the tools that help to find bugs or perform static analysis?
PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.

What are Python decorators?
A Python decorator is a specific change that we make in Python syntax to alter functions easily.
What is the difference between list and tuple?
The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed for e.g as a key for dictionaries.

How are arguments passed by value or by reference?
Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result you cannot change the value of the references. However, you can change the objects if it is mutable.

What is Dict and List comprehensions are?
They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.

What are the built-in type does python provides?
There are mutable and Immutable types of Pythons built in types Mutable built-in types
  • List
  • Sets
  • Dictionaries
Immutable built-in types
  • Strings
  • Tuples
  • Numbers
What is namespace in Python?
In Python, every name introduced has a place where it lives and can be hooked for. This is known as namespace. It is like a box where a variable name is mapped to the object placed.  Whenever the variable is searched out, this box will be searched, to get corresponding object.

What is lambda in Python?
It is a single expression anonymous function often used as inline function.

Why lambda forms in python does not have statements?
A lambda form in python does not have statements as it is used to make new function object and then return them at runtime.

What is pass in Python?
Pass means, no-operation Python statement, or in other words it is a place holder in compound statement, where there should be a blank left and nothing has to be written there.

In Python what are iterators?
In Python, iterators are used to iterate a group of elements, containers like list.

What is unittest in Python?
A unit testing framework in Python is known as unittest.  It supports sharing of setups, automation testing, shutdown code for tests, aggregation of tests into collections etc.

In Python what is slicing?
A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.

What are generators in Python?
The way of implementing iterators are known as generators.  It is a normal function except that it yields expression in the function.

What is docstring in Python?
A Python documentation string is known as docstring, it is a way of documenting Python functions, modules and classes.

How can you copy an object in Python?
To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.

What is negative index in Python?
Python sequences can be index in positive and negative numbers.   For positive index, 0 is the first index, 1 is the second index and so forth.  For negative index, (-1) is the last index and (-2) is the second last index and so forth.

How you can convert a number to a string?
In order to convert a number into a string, use the inbuilt function str().  If you want a octal or hexadecimal representation, use the inbuilt function oct() or hex().

What is the difference between Xrange and range?
Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.

What is module and package in Python?
In Python, module is the way to structure program. Each Python program file is a module, which imports other modules like objects and attributes.
The folder of Python program is a package of modules.  A package can have modules or subfolders.

Explain how to make Forms in python?
As python is scripting language forms processing is done by Python. We need to import cgi module to access form fields using FieldStorage class.
Every instance of class FieldStorage (for 'form') has the following attributes:
form.name: The name of the field, if specified.
form.filename: If an FTP transaction, the client-side filename.
form.value: The value of the field as a string.
form.file: file object from which data can be read.
form.type: The content type, if applicable.
form.type_options: The options of the 'content-type' line of the HTTP request, returned as a dictionary.
form.disposition: The field 'content-disposition'; None if unspecified.
form.disposition_options: The options for 'content-disposition'.
form.headers: All of the HTTP headers returned as a dictionary.
A code snippet of form handling in python:
import cgi
form = cgi.FieldStorage()
if not (form.has_key("name") and form.has_key("age")):
print "<H1>Name & Age not Entered</H1>"
print "Fill the Name & Age accurately."
return
print "<p>name:", form["name"].value
print "<p>Age:", form["age"].value

Describe how to implement Cookies for Web python?
A cookie is an arbitrary string of characters that uniquely identify a session.
Each cookie is specific to one Web site and one user.
The Cookie module defines classes for abstracting the concept of cookies. It contains following method to creates cookie
  • Cookie.SimpleCookie([input])
  • Cookie.SerialCookie([input]
  • Cookie.SmartCookie([input])
for instance following code creates a new cookie ck-
import Cookie
ck= Cookie.SimpleCookie ( x )

Describe how to use Sessions for Web python?
Sessions are the server side version of cookies. While a cookie preserves state at the client side, sessions preserves state at server side.
The session state is kept in a file or in a database at the server side. Each session is identified by a unique session id (SID). To make it possible to the client to identify himself to the server the SID must be created by the server and sent to the client whenever the client makes a request.
Session handling is done through the web.session module in the following manner:
import web.session session = web.session.start( option1, Option2,... )
session['myVariable'] = 'It can be requested'

Describe how exceptions are handled in python?
Errors detected during execution of program are called exceptions. Exceptions can be handled using the try..except statement. We basically put our usual statements within the try-block and put all our error handlers in the except-block.
try…except demo code:
>>> while True:
try:
         x = int(raw_input("Enter no. of your choice: "))
         break
except ValueError:
         print "Oops! Not a valid number. Attempt again"
Enter no. of your choice: 12ww
Oops! Not a valid number. Attempt again
Enter no. of your choice: hi there
Oops! Not a valid number. Attempt again
Enter no. of your choice: 22
>>> 
Request to Download PDF

Post A Comment:

0 comments: