Convert Matlab Code To Python