if it has no materials yet, just append a material datablock
mat = bpy.data.materials[‘Material1’]
Object.data.materials.append(mat)
Otherwise get the material’s index (granted the desired material is in a material slot already) and assign it to all Polygon.material_index
me = ob.data
index = me.materials.find("Material1")
if index == -1:
index = len(me.materials)
me.materials.append(mat)
for poly in me.polygons:
poly.material_index = index
You can use bpy.data.materials.get(“Material1”) if you want, but only if you don’t know for sure it exists. Instead of raising an exception, it will return None or another default value (second argument of get()).