From: Thomas Jollans on
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.
From: D'Arcy J.M. Cain on
On Sat, 07 Aug 2010 13:48:32 +0200
News123 <news1234(a)free.fr> wrote:
> It makes sense in assembly language and even in many byte code languages.
> It makes sense if you look at the internal representation of unsigned
> numbers (which might become an index)
>
> For a complete beginner common sense dictates differently and there
> might be confusion why the second element in a list has index 1.

Would said beginner also be surprised that a newborn baby is zero years
old or would it be more natural to call them a one year old? Zero
based counting is perfectly natural.

--
D'Arcy J.M. Cain <darcy(a)druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
From: Roald de Vries on
On Aug 7, 2010, at 2:54 PM, D'Arcy J.M. Cain wrote:
> On Sat, 07 Aug 2010 13:48:32 +0200
> News123 <news1234(a)free.fr> wrote:
>> It makes sense in assembly language and even in many byte code
>> languages.
>> It makes sense if you look at the internal representation of unsigned
>> numbers (which might become an index)
>>
>> For a complete beginner common sense dictates differently and there
>> might be confusion why the second element in a list has index 1.
>
> Would said beginner also be surprised that a newborn baby is zero
> years
> old or would it be more natural to call them a one year old? Zero
> based counting is perfectly natural.

A new born baby is in his/her first year. It's year 1 of his/her life.
For this reason, also "the year 0" doesn't exist. From the fact that a
baby can be half a year old, you derive that arrays should have floats
as indices?
From: Steven D'Aprano on
On Sat, 07 Aug 2010 14:00:59 +0200, Thomas Jollans wrote:

> 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."

I'll have you know that there are still some Pascal programmers in the
world, thank you.



> 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.

It didn't take me long to get used to thinking in zero-based indexes, but
years later, I still find it hard to *talk* in zero-based indexes. It's
bad enough saying that the first element in a list in the zeroth element,
but that the second element is the first makes my head explode...


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

Er, what's the -1th element of an infinite sequence?



--
Steven
From: D'Arcy J.M. Cain on
On Sat, 7 Aug 2010 15:37:23 +0200
Roald de Vries <downaold(a)gmail.com> wrote:
> > Would said beginner also be surprised that a newborn baby is zero
> > years
> > old or would it be more natural to call them a one year old? Zero
> > based counting is perfectly natural.
>
> A new born baby is in his/her first year. It's year 1 of his/her life.
> For this reason, also "the year 0" doesn't exist. From the fact that a
> baby can be half a year old, you derive that arrays should have floats
> as indices?

No. You are giving me math and logic but the subject was common
sense. Common usage counts ages as years with the second year called
"one year old" so zero based counting is common. We don't tell Aunt
Martha that little Jimmy is in his third year. We say that he is two
years old and Aunt Martha, a non-programmer, understands exactly what
we mean. Using one-based counting (first year, second year, etc.)
would be the unnatural thing, would confuse Aunt Martha and make her
spoil her apple pie and no one wants that.

--
D'Arcy J.M. Cain <darcy(a)druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.