gmpy supports rationals. Just say q = gmpy.mpq(1) to initialize q to 1/1. Any arithmetic you do on it after that (except with floating point operands) will give rational results.