Thursday, February 19, 2026
HomeLanguagesPython | sympy.count() method

Python | sympy.count() method

Using the count() method in the sympy module, we can count the number of variables used in the mathematical function. count() method returns the number of variables used in the mathematical function.

Syntax : sympy.count(x)

Return : the count of variables.

Code #1: With the help of the below examples, we can clearly understand that using sympy.count() method we can count the number of variables in a given expression. 

Python3




# importing sympy library
from sympy import *
 
# taking symbols
x, y, z = symbols('x y z')
 
# calling count() method on expression
geek = sqrt((x + 1)**2 + x).count(x)
print(geek)


Output: 

2

  Code #2: 

Python3




# importing sympy library
from sympy import *
 
# taking symbols
a, b = symbols('a b')
 
# calling count() method on expression
geek = (log(a) + log(b) + log(a)*log(1 / b)).count(log)
print(geek)


Output: 

4

  Code #3: 

Python3




# importing sympy library
from sympy import *
 
# taking symbols
a, b = symbols('a b')
 
# calling count() method on expression
geek = (log(a * b**(1 - log(a)))).count(b)
print(geek)


Output: 

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

1 COMMENT

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS