python – How to find the y-intercept using sympy?

python – How to find the y-intercept using sympy?

The y-intercept just means that you substitute 0 for x, so just do f.subs(x, 0).

Try using sympy.coeff, here, so like this:

Y-intercept as Coordinates

from sympy import Symbol

x = Symbol(x)
f = x**2-3

def return_y_intercept(f):
   return [0,f.coeff(x,0)] #return coordintes of y-intercept

print return_y_intercept(f)

Output:

0,-3

Y-intercept:

from sympy import Symbol

x = Symbol(x)
f = x**2-3

def return_y_intercept(f):
   return [f.coeff(x,0)] #return just the y-intercept

print return_y_intercept(f)

Output:

-3

try it on the online sympy interpreter here

python – How to find the y-intercept using sympy?

Leave a Reply

Your email address will not be published.