2 answers

You should use the method substitute_function instead of subs and use a symbol different from p to denote p(r) (otherwise there is a confusion between the function p and its value at r). Besides the semicolons at the end of each line are not necessary in Python and the line v = var('v') is useless, since the Python variable v is redeclared in the next line. So basically, your code should be: