How to select with python code only vertical edges from cube?
The dot product
If two vectors are parallel then the dot product of their normalized direction will be 1 if in same direction (angle between 0) or -1 in opposite direction -1 (angle between 180 degrees)
Checking that the absolute value of dot product is around 1 will find edges defined by the vertex coordinate difference regardless of order.
Have used this method in example here Subdividing cubes at different intervals
So similarly
    def aligned(e, axis, TOL=1e-5):
        axis = Vector(axis).normalized()
        dir = (e.verts[1].co - e.verts[0].co).normalized()
        return (1 - abs(dir.dot(axis))) &lt; TOL

    # select if aligned with local z axis.
    for e in bm.edges:
        e.select_set(aligned(e, (0, 0, 1)) 

    # select if aligned with global z axis
    axis = ob.matrix_world.inverted() @ Vector((0, 0, 1)) # local space
    for e in bm.edges:
        e.select_set(aligned(e, axis)) 

(if doubles in mesh will need to deal with zero length edges.)
Hardcode for default cube.
if this is a freshly added  default cube can rely on the edges having the same index each time, and could select based on index.
geom=[e.index for e in bm.edges if aligned(e, (0, 0, 1))]
print(geom)

Outputs [1, 3, 6, 9]
so the  bmesh edges of the default cube in $Z$ dir are
bm.edges.ensure_lookup_table()
vertical_edges = [bm.edges[i] for i in (1, 3, 6, 9)]