A few questiosn about encoding

Antoon Pardon antoon.pardon at rece.vub.ac.be
Fri Jun 14 09:52:38 EDT 2013


Op 14-06-13 14:59, Nick the Gr33k schreef:

> On 14/6/2013 1:50 μμ, Antoon Pardon wrote:
>> Python works with numbers, but at the moment
>> it has to display such a number it has to produce something
>> that is printable. So it will build a string that can be
>> used as a notation for that number, a numeral. And that
>> is what will be displayed.
> so a number is just a number but when this number needs to be displayed 
> into a monitor, then the printed form of that number we choose to call 
> it a numeral?
> So, a numeral = a string representation of a number. Is this correct?
Yes, when you print an integer, what actually happens is something along
the following algorithm (python 2 code):


def write_int(out, nr):
    ord0 = ord('0')
    lst = []
    negative = False
    if nr < 0:
        negative = True
        nr = -nr
    while nr:
        digit = nr % 10
        lst.append(chr(digit + ord0))
        nr /= 10
    if negative:
        lst.append('-')
    lst.reverse()
    if not lst:
        lst.append('0')
    numeral = ''.join(lst)
    out.write(numeral)

-- 
Antoon Pardon








More information about the Python-list mailing list