Python - Set material to material slot
This code creates 10 materials and appends it to the active object (should be a mesh). The diffuse color of every material is random. The .material_index poperty is set to the new materials by index (even if the object has materials assigned before the operation).

import bpy
from random import random

ob = bpy.context.object
me = ob.data

mat_offset = len(me.materials)
mat_count = 10

mats = []
for i in range(mat_count):
    mat = bpy.data.materials.new("Mat_%i" % i)
    mat.diffuse_color = random(), random(), random()
    me.materials.append(mat)

# Can't assign materials in editmode
bpy.ops.object.mode_set(mode='OBJECT')

i = 0
for poly in me.polygons:
    if poly.select:
        poly.material_index = i % mat_count + mat_offset
        i += 1


If you want to use UV mapped textures, you'll also have to deal with me.uv_layersand me.uv_textures. It's not enough to assign materials to faces, you also need to set the texture image for every face.