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