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:

 `# 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:

# 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]
```

My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.