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 sympyfrom sympy.ntheory.factor_ import digitsn = 7524b = 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 sympyfrom sympy.ntheory.factor_ import digitsn = 33b = 2# Use digits() methoddigits_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 sympyfrom sympy.ntheory.factor_ import digitsn = -35b = 10# Use digits() methoddigits_n_b = digits(n, b) print("Digits of {} in base {} = {} ".format(n, b, digits_n_b)) |
Output:
[-10, 3, 5]



