Python "why" questions

Thomas Jollans thomas at jollans.com
Sat Aug 7 08:00:59 EDT 2010


On 08/07/2010 05:05 AM, Default User wrote:
>>From "the emperor's new clothes" department:
> 
> 1)  Why do Python lists start with element [0], instead of element [1]? 
> "Common sense" would seem to suggest that lists should start with [1]. 

As others have pointed out, there is a nice argument to be made for
zero-based indices. However, the killer reason is: "it's what everybody
else does." As it stands, the only perceived problem with zero-based
indices is that it's one of the many tiny confusions that new
programmers face. On the other hand, it's the way nearly every other
popular programming language does it, and therefore, it's the way almost
every programmer likes to think about sequences.

Also, it has the nice property that, for an infinite sequence, every
integer makes sense as an index (in Python).

> 
> 2)  In Python 3, why is print a function only, so that: print "Hello,
> World" is not okay, but it must be print("Hello, World") instead? 
> (Yeah, I know: picky, picky . . . )
> 
> 3)  In Python 3, why does 2.0 / 3.0 display as 0.6666666666666666, but 8
> * 3.57 displays as 28.56 (rounded off to 2 decimal places)?  And yet, in
> Python 2.6, 8 * 3.57 displays as 28.559999999999999?

0:pts/3:~% python3.1
Python 3.1.2 (release31-maint, Jul  8 2010, 09:18:08)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 28.56
28.56
>>>
0:pts/3:~% python2.6
Python 2.6.6rc1+ (r266rc1:83691, Aug  5 2010, 17:07:04)
[GCC 4.4.5 20100728 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 28.56
28.559999999999999
>>>
0:pts/3:~%

same number - why use more digits if you can avoid it? Python 3 is smart
enough to avoid it.

> 
> And we wonder why kids don't want to learn to program. 

Don't kids want to learn to program? Many don't, a fair bunch do. It's
the same for any other art. Also, the only people that realize this kind
of "issue" are those that have already learned programming.



More information about the Python-list mailing list