How to get average z location of selected vertices in python?
Bmesh module solutions for both tasks (edit-mode only so far):

import bpy
import bmesh

ob = bpy.context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)

loc = [v.co.z for v in bm.verts if v.select]
if loc:
    avg = sum(loc) / len(loc)
    print(avg)

bpy.ops.mesh.select_all(action='DESELECT')

for v in bm.verts:
    if v.co.x &lt; 2 and v.co.x &gt; 1:
        v.select = True
bm.select_flush(True)