- Q:What is PYTHONPATH?
A: Python uses the directories listed in PYTHONPATH to find a module. - Q: How can I see the PYTHONPATH on my system?
A: Several ways:- From the shell prompt: echo $PYTHONPATH
- Programmatically in python itself:
>>> import sys
>>> sys.path
['', '', '', '/usr/lib/python2.6/dist-packages/pygame/tests', '/usr/lib/python2.6/dist-packages/pygame', '', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0']
- Q: How can I add a directory to the PYTHONPATH?
A: Programmatically, in python itself:#!/usr/bin/python
import sys
sys.path.append('path/to/my/modules/')
# now import a module/.py file that resides in "path/to/my/modules"
import my_module - Q: What are some standard python modules?
A: Take a look at the Python Standard Library Reference Manual: http://docs.python.org/library/index.html
Some standard modules are: os, sys, string, re, socket, datetime, math, commands [a useful wrapper around ps.popen() for running unix commands], httplib, urllib, json, ssl, thread, getopt, optparse, random - Q: What are packages in python?
A: Packages are used to organize and group modules. Packages are just folders of modules with a special init.py file that indicates to Python that this folder is special because it contains Python modules. Packages are just a convenience to hierarchically organize modules. - Q: What is init.py used for?
A: Source: http://effbot.org/pyfaq/what-is-init-py-used-for.htm
It is used in marking a directory as a package directory. If you have the files:
mydir/mypackage/init.py
mydir/mypackage/mymodule.py
and mydir is on the PYTHONPATH, you can import the code in mymodule.py via from mypackage import mymodule
or via import mypackage.mymoduleIf you remove the init.py file from the mypackage directory, Python will NOT look for modules inside the mypackage directory, and you will not be able to import the mymodule module.
The init.py file is usually empty. Furthermore, init.py is the first file to be loaded in a package, so you can use it to execute initialization code that you want to run each time a module is loaded, or specify the submodules to be exported.
- Q: What is the init function for?
A: init is similar to a constructor for a class in Python. It is a way to specify default values for an object's data members when you create an object. These values are set in the class definition.The init() method is called immediately after an instance of the class is created. It would be tempting — but technically incorrect — to call this the “constructor” of the class. It’s tempting, because it looks like a C++ constructor (by convention, the init() method is the first method defined for the class), acts like one (it’s the first piece of code executed in a newly created instance of the class), and even sounds like one. Incorrect, because the object has already been constructed by the time the init() method is called, and you already have a valid reference to the new instance of the class.
The first argument of every class method, including the init() method, is always a reference to the current instance of the class, called "self".
Example:
class Point:
def __init__ (self, arg1=0, arg2=0):
self.x = arg1
self.y = arg2
def display(self):
print "Point::display(): x=" + str(self.x) + " and y=" + str(self.y)
p1 = Point(5, 8);
p2 = Point();
p1.display();
p2.display();results in:
Point::display(): x=5 and y=8
Point::display(): x=0 and y=0
- Q: What is main used for?
A: source: http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm
The if name == "main": ... trick exists in Python so that our Python files can act as either reusable modules, or as standalone programs.Example:
square.py :def square(x):
return x*x
if __name__ == '__main__' ;
print "square.py | the square of 10 is: " , square(10)test.py:
import square
print "test.py | the square of 20 is: " , square(20)execution:
$ python square.py
square.py | the square of 10 is: 100
$ python test.py
test.py | the square of 20 is: 400 - Q: What is a lambda function? Explain with an example. Typical usage?
A: A lambda function is a shorthand for an anonymous function.
The form is lambda <arguments>: <resultant-expression>
- Q: What is the map function? Explain with an example. Typical usage?
A: map applies a function to every element of a list and returns the resultant list.
PNEMONIC: Hence it "maps" a function to every element of a list.
Example:>>> map ( lambda x: x*2, [1,2,3] )
[2, 4, 6] - Q: What is the reduce function?
A: reduce takes as input a function and a list. The function must take exactly two arguments. reduce applies the function to the first two successive elements of the list from left to right, and then replaces the first two elements with the result of the function.
PNEMONIC: Hence it "reduces" the number of elements in the list by one on each operation, until eventually only one element is left.Example:>>> # this effectively sums the elements in the list by doing ((((1+2)+3)+4)+5)
>>> reduce( lambda x,y : x+y, [1,2,3,4,5] )
15 - Q: What is the filter function?
A: filter applies a function to every element of a list. Unlike, map, however, the function must return either True or False for each check. filter returns a list of all elements that pass this check.
PNEMONIC: Hence it "filters" those elements that pass the test of the function.Example:
>>> # get those elements in the list that are divisible by 3
>>> filter ( lambda x : x%3==0 , [1,2,3,4,5,6])
[3,6] - Q: What is a list comprehension? Explain with an example. Typical usage?
- Q: What is pass used for?
A: The pass statement in Python is like a empty set of curly braces ({}) in Java or C. - Q: How do you create an empty tuple?
A: t = () - Q: How do you create a tuple containing one element?
A: t = (something, ) #note the comma at the end - this is the only way to specify a single-element tuple - Q: How can you use the and-or trick safely (to simulate a C-style ternary expression)?
A: <exprtotest> and [a] or [b])[0] - Q: How can you use a much-nicer and more-readable ternary style expression in Python?
A: Example: map(lambda x:x*2, range(0,20)) if (2 > 1) else "" - Q: How can you delete an element or a slice of elements from a list?
A: Using the del statement
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[] - Q: What are ".pyc" files and when are they generated?
A: Importing a module is a relatively costly affair, so Python does some tricks to make it faster. This .pyc file is useful when you import the module the next time from a different program - it will be much faster since a portion of the processing required in importing a module is already done. Also, these byte-compiled files are platform-independent.
source - Q: What is the use of the inbuilt dir function?
A: You can use the built-in dir function to list the identifiers that an object defines. When you supply a module name to the dir() function, it returns the list of the names defined in that module. When no argument is applied to it, it returns the list of names defined in the current module.
source - Q: Explaing duck-typing wrt Python.
A: Duck-typing can be simply explained as, "As long as you get the job done, we don't care who your parents are."In other words, as long as an object can perform these actions:
- quackLikeADuck()
- walkLikeADuck()
- swimLikeADuck()
then that object is a Duck for Python. It doesn't matter which class the object is derived from (who its parents are). This greatly improves polymorphism and thus flexibility. Duck-typing avoids tests using type() or isinstance()
Example: The canonical examples of duck typing in Python are file-like classes. You can use cStringIO to handle a string like a file, or GzipFile to handle a gzipped-object like a file. Similarly you can treats sockets like files too. However, sockets lack the 'tell()' method and so cannot be used everywhere that a file can be used. This shows the flexibility of duck typing: a file-like object needs to implement methods only that it is able to. Thus it can only be used in situations where it makes sense. Where the object is expected to support the 'tell()' method, and it doesn't, it can throw a "TellException" instead, which makes it easy to handle the unsupported-method-exception in the calling code and then move on. This is called as EAFP: "Easier to Ask Forgiveness than to ask for Permission" style programming.
petrichor (/'pe - tri - kor'/) is the familiar scent of rain on dry earth
this tech blog is the wafting fragrance of my geeky outpourings, one post at a time
Tuesday, August 2, 2011
python interview questions - part 1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment