Lock X and Y axis of vertices in edit mode from python
Once you start moving an object you can press Z to lock all motion to the z axis (preventing changes in x or y).

https://docs.blender.org/manual/en/dev/editors/3dview/object/editing/transform/control/precision/axis_locking.html