How to change to constant interpolation mode from a python script?
slightly simple option (probably not the best in the long run)

A toggle inverts, if all curves are selected it will select the curves that are not selected. The solution might be as simple as this.

bpy.ops.graph.select_all_toggle(invert=False)


broader answer (which is hopefully more useful)

Easy enough, but perhaps not very obvious.

kf = bpy.data.objects['Cube'].animation_data.action.fcurves[0].keyframe_points[0]
kf.interpolation = 'CONSTANT'


or for Keyframes on all F-Curves of that object

import bpy

obj = bpy.data.objects['Cube']
fcurves = obj.animation_data.action.fcurves
for fcurve in fcurves:
    for kf in fcurve.keyframe_points:
        kf.interpolation = 'CONSTANT'


Without seeing your code I can only assume things about it. You can use the Outliner (DataBlocks view) to get a reasonable idea of where to look in the API for these things.



Addendum

If the script seems to behave slowly, it might not be the interpolation but the way the script adds keyframes. I Might be wrong but I assume you are using something like this to add keyframes:

bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
# this does more than just add keyframes


This would be faster: 

import bpy

positions = (0,0,2),(0,1,2),(3,2,1),(3,4,1),(1,2,1)
start_pos = (0,0,0)

bpy.ops.mesh.primitive_uv_sphere_add(segments=32, size=0.3, location=start_pos)
bpy.ops.object.shade_smooth()
ob = bpy.context.active_object

frame_num = 0

for position in positions:
    bpy.context.scene.frame_set(frame_num)
    ob.location = position
    ob.keyframe_insert(data_path="location", index=-1)  # &lt;-- this
    frame_num += 10


Setting the interpolation type at creation time:

for position in positions:
    bpy.context.scene.frame_set(frame_num)
    ob.location = position
    ob.keyframe_insert(data_path="location", index=-1)

    for fcurve in ob.animation_data.action.fcurves:
        kf = fcurve.keyframe_points[-1]
        kf.interpolation = 'CONSTANT'
    frame_num += 10