[Tutor] Tutorial indicating different output

Alan Gauld alan.gauld at btinternet.com
Fri May 9 00:33:46 CEST 2008


"Eric Morey" <eric at glodime.com> wrote


>        >>> 2.0/5 # get decimal quotient
>        0.4
>        >>> 2/5.0 # do it again
>        0.4
>        >>>
> 
> http://www.dickbaldwin.com/python/Pyth0004-fig10.htm
>
>
> Howerver, using the python shell in my terminal, I get:
>        >>> 2.0/5 # get decimal quotient
>        0.40000000000000002
>        >>> 2/5.0 # do it again
>        0.40000000000000002
>        >>>

Looks like the author cheated a little.
If you explicitly print the result:

 >>> print 2.0/5 # get decimal quotient
0.4

It should work as expected.

Without the print Python uses a slightly different set of
formatting rules for values. (print uses str() while evaluating
uses repr(), but you don;t need to worry about that yet! :-)

My advice to beginners is to always use print for consistent
results even though it is more typing.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list