Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python change base function

>>>import numpy as np
>>>np.base_repr(10, base=3)
'101'
Comment

change base python

def base_repr(number, base=2, padding=0):
    """
    Return a string representation of a number in the given base system.
    Parameters
    ----------
    number : int
        The value to convert. Positive and negative values are handled.
    base : int, optional
        Convert `number` to the `base` number system. The valid range is 2-36,
        the default value is 2.
    padding : int, optional
        Number of zeros padded on the left. Default is 0 (no padding).
    Returns
    -------
    out : str
        String representation of `number` in `base` system.
    See Also
    --------
    binary_repr : Faster version of `base_repr` for base 2.
    Examples
    --------
    >>> np.base_repr(5)
    '101'
    >>> np.base_repr(6, 5)
    '11'
    >>> np.base_repr(7, base=5, padding=3)
    '00012'
    >>> np.base_repr(10, base=16)
    'A'
    >>> np.base_repr(32, base=16)
    '20'
    """
    digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    if base > len(digits):
        raise ValueError("Bases greater than 36 not handled in base_repr.")
    elif base < 2:
        raise ValueError("Bases less than 2 not handled in base_repr.")

    num = abs(number)
    res = []
    while num:
        res.append(digits[num % base])
        num //= base
    if padding:
        res.append('0' * padding)
    if number < 0:
        res.append('-')
    return ''.join(reversed(res or '0'))
Comment

PREVIOUS NEXT
Code Example
Python :: tribonacci sequence python 
Python :: scientific notation to decimal python 
Python :: add path python sys 
Python :: discord.py commands.group 
Python :: removing a channel from aconda 
Python :: python program to multiplies all the items in a list using function 
Python :: python get time difference in milliseconds 
Python :: gpu training tensorflow 
Python :: time date in pandas to csv file 
Python :: update windows wallpaper python 
Python :: argparse example python pyimagesearch 
Python :: how to change the rate of speech in pyttsx3 
Python :: Scrape the text of all paragraph in python 
Python :: matplotlib set number of decimal places 
Python :: create pdf from images python 
Python :: cross validation python 
Python :: django delete session 
Python :: python csv add row 
Python :: subtract one list from another python 
Python :: change text color docx-python 
Python :: decode base64 with python 
Python :: schedule asyncio python 
Python :: printing a range of no one line in python 
Python :: python draw polygon 
Python :: python temporaty files 
Python :: get information about dataframe 
Python :: Geopandas to SHP file 
Python :: playsound 
Python :: rename columns in datarame pandas 
Python :: multivariate outlier detection python 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =