Add a directory to the python path in Linux

To add a directory to the python path in Linux you can add the path to the startup script in .profile or .bashrc (dependig on which shell you are using) .These files will be found in your home directory. So open up a terminal (Ctrl+Alt+T) and type pwd to make sure you are in your home directory. Then type:

cp .profile .profile_old


Find your python path (Windows)

>>> import sys, pprint

>>> pprint.pprint(sys.path)

This returns a list of places where python will look for modules, so you can save a module in any of these places(although you should create your own folder and add this to your path for any modules you create)  and import a module  using import








Add Python to your Windows environment variable

If you don’t want to change the environment variables on your machine you can run your scripts from the command prompt by specifying the full path to the python.exe like this;


Using Help()

Type help() for interactive help, or help(object) for help about object.
>>> help()
Welcome to Python 2.7!  This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".


Making a windows executable for your python program

There are a number of applications that let you create a windows executable from your python programs/scripts. The one I found most user friendly was PyInstaller which you can download from their website here.

Once downloaded unpack/unzip all files to a path of you choice. For the purpose of this document I put mine in the path C:\Python27\pyinstaller.

For Windows (32/64bit), Linux (32/64bit) and Mac OS X (32/64bit) precompiled boot-loaders are available. So the installation is complete now.



An exception is what python uses to flag a exceptional behavior in a program. These are objects that if not dealt with by the program will terminate the program with an error or traceback.

>>> 1/0
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: integer division or modulo by zero

As you can see a ZeroDivisionError has been raised. To see a list of all the exception available to you use:

>>> import exceptions


Creating A Class

To make a new style class simply subclass the built in class object

class NewStyleClass(object):
some methods here

To initialize the class automatically we use the constructor by creating a method using __init__

class MyClass(object):
def __init__(self):
myVar = 78

>>> c = MyClass()
>>> c.myVar



Polymorphism comes from two Greek words meaning ‘many shapes’ and is best explained by a real world example. The feet of a human are a very different shape to that of a centipede, but they have basically the same function, walking. So we can refer to the feet of a human, centipede, horse, or frog without confusion. Polymorphism allows the programmer to give the same names to methods and attributes that play similar roles in distinctive classes.
As an example we will use a built-in operator, function or method on an object without knowing what kind of object it is.


Lambda Expressions

This is a small function with no name that contains an expression and returns its result. We will use it in the built in function filter: filter(function, iterable).

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> filter(lambda n: n % 2 == 0, numbers)
[2, 4, 6, 8, 10]



This is where a function calls itself. Let’s look at a classic example of recursion, the power of something. First we define the problem:

    power(x, 0) is 1 for all numbers x.
    power(x, n) for n > 0 is the product of x and power(x, n-1).

def power(x, n):
if n == 0:
return 1
return x * power(x, n-1)



Subscribe to RSS - Python