Thursday, July 30, 2026
HomeLanguagesPython | sympy.compare() method

Python | sympy.compare() method

With the help of sympy.compare() method, we can compare the variables and it will return 3 values i.e -1 for smaller, 0 for equal and 1 for greater by using sympy.compare() method.

Syntax : sympy.compare()
Return : Return the value of comparison i.e -1, 0, 1.

Example #1 :
In this example we can see that by using sympy.compare() method, we are able to compare the variables and return 3 values i.e -1 for smaller, 0 for equal and 1 for greater.




# import sympy
from sympy import *
x, y = symbols('x y')
  
# Use sympy.compare() method
gfg = x.compare(y)
    
print(gfg)


Output :

-1

Example #2 :




# import sympy
from sympy import *
x, y = symbols('x y')
  
# Use sympy.compare() method
gfg = x.compare(x)
    
print(gfg)


Output :

0

Example #3 :




# import sympy
from sympy import *
x, y = symbols('x y')
  
# Use sympy.compare() method
gfg = y.compare(x)
    
print(gfg)


Output :

1

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS