[Tutor] a print puzzle

Alan Gauld alan.gauld at btinternet.com
Thu Dec 2 12:13:08 CET 2010


"Richard D. Moores" <rdmoores at gmail.com> wrote

>>>> s = [.1,.1,.1,.1,.1,.1,.1,.1,.1,.1]
>>>> sum(s)
> 0.9999999999999999

This uses repr() to display the result

>>>> print(sum(s))
> 1.0      Why?

This uses str() to display the result.

In many cases str() calls repr() but in other cases str() is a more 
user
friendly format. repr() tends to be a developers eye view of things.
This is one reason that in my tutorial I always use print() to display
results. The output is more newbie friendly that way :-)

I suspect the details will be revealed by a search for documentation
on __repr__

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list