And why is it necessary to check this condition ,  in the first piece of code?

    if ypart == y and xpart.as_base_exp()[0] == x: