HomeLanguagesPython | sympy.digits() method

Python | sympy.digits() method

With the help of sympy.digits() method, we can find the digits of a given integer in any given base in SymPy.

Syntax: digits(n, t=10) Parameter: n – It denotes an integer. b – It denotes an base integer(optional). Default for b is 10. Returns: Returns a list of the digits of n in base b. The first element in the list is b (or -b if n is negative).

Example #1: 

Python3




# import digits() method from sympy
from sympy.ntheory.factor_ import digits
 
n = 7524
b = 10
 
# Use digits() method
digits_n_b = digits(n, b)
     
print("Digits of {} in base {} =  {} ".format(n, b, digits_n_b))


Output:

Digits of 7524 in base 10 =  [10, 7, 5, 2, 4] 

Example #2: 

Python3




# import digits() method from sympy
from sympy.ntheory.factor_ import digits
 
n = 33
b = 2
 
# Use digits() method
digits_n_b = digits(n, b)
 
print(“Digits of {} in base {} = {} “.format(n, b, digits_n_b))


 Output:

Digits of 33 in base 2 =  [2, 1, 0, 0, 0, 0, 1] 

Example #3:

   If the number is negative, the negative sign will be placed on the base (which is the first element in the returned list)

Python3




# import digits() method from sympy
from sympy.ntheory.factor_ import digits
 
n = -35
b = 10
 
# Use digits() method
digits_n_b = digits(n, b)
     
print("Digits of {} in base {} = {} ".format(n, b, digits_n_b))


Output:

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

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS