You can usually retrieve the active object with:
If you need to take the view layer into account:
Chances are your view layer is already set, but if the view layer needs to be set, you can first do:
bpy.context.window.view_layer = view_layer
To make sure the active object is also a selected object, you can do this:
my_object = bpy.context.view_layer.objects.active # assign variable
my_object.select_set(True) # select the object
Setting an object as active is a similar process. See this answer to learn about that.
Many operations require both an active object and at least one additional selected object.