How to set the newly added material to an object as "active" (or selected) material I add a new material created by this little example :

ob = bpy.context.active_object
mat = bpy.data.materials.new(name="Example Material")
ob.data.materials.append(mat)


I noticed that the material is added but not active, as in the normal practice of adding blender materials bpy.ops.object.material_slot_add ()
I was wondering how to do this simple step, I tried with ob.active_material = mat, but is wrong:

Edit:

This is what I get, it creates confusion and does not set the active slot correctly:



I would like to get this:

Note that the newly created slot is selected by default (which is what I would like to get)
To select the last appended material (which corresponds to a material slot in the UI), you can set active material index:

import bpy

ob = bpy.context.active_object
mat = bpy.data.materials.new(name="Example Material")
ob.data.materials.append(mat)

# As appended the new material slot index is len - 1
ob.active_material_index = len(ob.material_slots) - 1