write inversion in python