Select object by dimension
Allow for precision errors.
In addition to @Secrop's answer, it is a good idea to allow for precision errors when testing equality of floats, ie 2.000001 is probably close enough to 2.
A commonly seen test to see if a value is within a tolerance TOL of 2
if abs(value - 2) &lt; TOL

or in the case here, using math.isclose
import bpy
from math import isclose

context = bpy.context

scene = context.scene

for ob in scene.objects:
    ob.select_set(
            isclose(ob.dimensions.z, 2, rel_tol=1e-6)
    )

which is  setting the selection of all objects in the scene to the test result.
Using
if some_test_is_true:
    ob.select_set(True)

will make no change to selection if all where selected beforehand.
Can extend a selection using or.
    ob.select_set(
            ob.select_get()
            or
            isclose(ob.dimensions.z, 2, rel_tol=1e-6)
            )

or, as shown in your answer, add other specs with and eg ob.type == 'MESH' and ...