# assign color polygon python API

how can I assign colours to a specific face/polygon in the python API? I have found a way of creating materials, but how can I use them through the API on chosen polygons of an object? Does an object have to be fully covered with a material before you can change that of individual parts?

The mesh doesn't have to be fully covered with a material, but it does have to have material slots(at least two) if you want to be able to change individual parts.

import bpy

obj = bpy.data.objects['Cube']
mat = bpy.data.materials['Red']
mat_slots = obj.material_slots
polys = obj.data.polygons

#add a material slot using a material
obj.data.materials.append(mat)

#remove material from material slot
mat_slots[0].material = None

#add the material slot that will be used for individual face
obj.data.materials.append(mat)

#apply material to one face
polys[0].material_index = 1