How to create a PyDriver in blender 2.7
your example can be implemented as in the following driver :



but for more complex drivers refer to this DOC page , the important part is that you can define your function and add it to driver namespace as in this example :

import bpy

def driverFunc(val):

    return val * val    # return val squared

bpy.app.driver_namespace['driverFunc'] = driverFunc    # add function to driver_namespace


now you can use it the driver scripted expression