Custom Search

Monday, February 20, 2012

python os.path and sys.path difference

>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/IPy-0.75-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/guppy-0.1.9-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/virtualenv-1.6.4-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/fbpy-0.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/Kivy-1.0.9-py2.7-linux-x86_64.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/pymodules/python2.7/libubuntuone', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
>>>
>>>
>>>
>>> os.path
module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>
>>>
>>>
>>>

Friday, February 10, 2012

different methods to find factorial in python

>>> def fact(x):
... if x == 0:
... return 1
... else:
... return x * fact(x-1)
...
>>>
>>> fact(4)
24


-----------------------

def a(n):
f = 1
for x in range(1, n+1):
f *= x
print "fact : ", f

a(4)

-----------------------

>>> n=4
>>> reduce(int.__mul__, range(1, n+1))
24

or

>>> fact = lambda n : reduce(int.__mul__, range(1, n+1))
>>> fact(4)
24

-----------------------

>>> n=4
>>> reduce(lambda x,y:x*y, range(1, n+1))
24

or

>>> fact = lambda n : reduce(lambda x,y:x*y, range(1, n+1))
>>> fact(4)
24

-----------------------

>>> int.__mul__(3,4)
12


>>> import operator
>>> n=4
>>> reduce(operator.mul, range(1, n+1))
24

or

>>> import operator
>>> fact = lambda n : reduce(operator.mul, range(1, n+1))
>>> fact(4)
24

-----------------------

>>> def factorial(n):
... x = n
... for j in range(1, n):
... x = j*x
... return x
...
>>>
>>> factorial(4)
24