News for hackers. Stuff that matters.
Re:Why Python uses 0-based indexing. It's because of slices by dpratt on Wednesday October 23, 2013 @05:46PM (#6601950)
This is not meant to be tongue-in-cheek - I thought this argument was settled ages ago. Are there any respected languages that use 1 based indexing? This is an honest question - every single one I've ever used (C, C++, Java, Scala, Python, JS, Objective-C, a bunch of academic ones) have been zero based. It's quite clear that it's the right solution, since the first element of an array is zero distance away from the beginning of the array.
Re:Why Python uses 0-based indexing. It's because of slices by tzs on Wednesday October 23, 2013 @08:15PM (#6602658)
> Are there any respected languages that use 1 based indexing?

Fortran, Lua, Mathematica, MATLAB, Julia, and Smalltalk among current languages. Among historically important languages, COBOL, Algol68, PL/I.

> It's quite clear that it's the right solution, since the first element of an array is zero distance away from the beginning of the array.

You think it is clear because you are used to thinking of array indexes as offsets. If you think of them as ordinal numbers for describing the position in a list, then it is clear they should start at 1. If someone's grocery list is:

   bread
   milk
   eggs
   flour
and you ask them "what item is #1 on your list?", most people will say bread.

If you look at that list I gave of 1-based languages, you might note that many of them are oriented toward math/science/engineering. In math, both 0-based and 1-based are used. For example, matrix elements are specified by row and column number, and both of those usually start at 1. On the other hand, polynomial coefficients usually start with 0.

Re:Why Python uses 0-based indexing. It's because of slices by EpicEng on Wednesday October 23, 2013 @10:04PM (#6602993)
Ignoring the fact that equating what "normal people" would think in contrast to what a trained professional would think in the context of said profession, 1-indexing leads to other problems.

foreach is great, until you need an index. When you do, 0-indexing is more natural. Take, for example, looping over an image.

  unsigned char *buf = get_image();
  for(int y = 0; y < height; ++y) {
    for(int x = 0; x < width; ++x) { 
      // simple
      unsigned char pixel = buf[y * width + x];
    }
  }
That doesn't work when y begins at 1. All of a sudden you need to add and/or subtract 1 everywhere to keep things in line. I used C here, but this applies to many languages in many different circumstances where an index/count is required.
Re:Why Python uses 0-based indexing. It's because of slices by EpicEng on Thursday October 24, 2013 @01:01AM (#6603491)
Can't edit now, but I meant to say:

"Ignoring the fact that equating what "normal people" would think in contrast to what a trained professional would think in the context of said profession isn't always an apt comparison..."

Re:Why Python uses 0-based indexing. It's because of slices by daviddlhg on Thursday October 24, 2013 @08:14AM (#6604749)
fortran really lets you start from any integer index, including negative ones, 1 is just a default if you omit the lower bound. But it's not half-open when you declare (or use slicing) i.e. it uses inclusive lower and upper bounds, so A(-17:-17, 10:10) has one element not none. It might or might not have been nicer if they'd been half-open. Or one could also imagine a start+length type declaration, but meh.

http://orion.math.iastate.edu/burkardt/papers/fortran_arrays...

Re:Why Python uses 0-based indexing. It's because of slices by esrauch on Wednesday October 23, 2013 @05:49PM (#6601974)
Lua is probably the strongest candidate for a respected 1-based language. Smalltalk, Fortran, and MATLAB are other examples of widely used languages that are 1 based.
Re:Why Python uses 0-based indexing. It's because of slices by desireco42 on Wednesday October 23, 2013 @08:03PM (#6602619)
I am going to learn Lua and use it much more now, thanks for pointing this out. As someone said, having 0 based indexes is inhuman way of dealing with arrays, only made some sense in C, others just copied.
Re:Why Python uses 0-based indexing. It's because of slices by kevincrane on Wednesday October 23, 2013 @05:51PM (#6601984)
Matlab does (or at least it did when I used it for a class a few years ago). I was trying to iterate through some array for a project and kept hitting an error, and was totally stumped until some EE major came over and fixed it. "Haha did you forget how to count? You're starting the index at 0, that's the problem."
Re:Why Python uses 0-based indexing. It's because of slices by ArbitraryLimits on Wednesday October 23, 2013 @05:54PM (#6602012)
Did the same guy remind you that "i" is a poor choice for an index in Matlab because it's actually sqrt(-1)? And _then_ tell you that you're supposed to do array reshaping instead of loops in Matlab? Because if the same guy had hit me with all three I might have killed him.
Re:Why Python uses 0-based indexing. It's because of slices by ihuman on Wednesday October 23, 2013 @07:03PM (#6602374)
I had to use matlab the other day. It still starts at one. The first time I used it, that confused me too.
Re:Why Python uses 0-based indexing. It's because of slices by aidos on Wednesday October 23, 2013 @08:34PM (#6602729)
I've just started using Octave (for the coursera machine learning course) and it also starts at 1. Takes a few exercises to reaclimatise. More annoying is that it doesn't auto broadcast matrix operations like numpy does (and the slicing doesn't feel as powerful either).
Re:Why Python uses 0-based indexing. It's because of slices by Dylan16807 on Wednesday October 23, 2013 @07:02PM (#6602370)
Hahahaha respecting the matlab scripting language, good one.
Re:Why Python uses 0-based indexing. It's because of slices by ArbitraryLimits on Wednesday October 23, 2013 @06:00PM (#6602035)
> since the first element of an array is zero distance away from the beginning of the array.

That's much less of a win in any language but C, where "array" means something more than "pointer." If you're checking array bounds on every access then the extra assembler instruction to subtract one from your index doesn't matter in comparison, whereas if you're just dereferencing a pointer it could potentially double the cost of array accesses (two instructions instead of one if your instruction set has base + offset addressing modes).

Re:Why Python uses 0-based indexing. It's because of slices by dpratt on Wednesday October 23, 2013 @06:31PM (#6602208)
I'm just thinking logically - not even on a pointer level. If I'm doing index/offset math to calculate an array position, it's foolish to start at 1 instead of 0.
Re:Why Python uses 0-based indexing. It's because of slices by anonymoushn on Wednesday October 23, 2013 @06:37PM (#6602230)
Adding things that aren't conceptually indices to indices works the same way in both conventions (so a[idx] and a[idx+k] have k-1 things in between). You miss out on stuffing multiple dimensions into one with a[x*xlen+y], but there's generally no reason to do that.

Could you elaborate about the uses you have in mind?.

Re:Why Python uses 0-based indexing. It's because of slices by anonymoushn on Wednesday October 23, 2013 @05:54PM (#6602008)
Matlab does this, and Julia is following suit out of a desire to be attractive to Matlab users. I would really rather it didn't.

The languages that do this seem to pretty uniformly use closed intervals, so that [1:length] is the whole thing, while in python [0:length] is.

Re:Why Python uses 0-based indexing. It's because of slices by agrona on Wednesday October 23, 2013 @05:53PM (#6602000)
FORTRAN and Algol60 are also not-0-indexed.

From the exple.tive.org page Guido links to:

> On top of that other languages that antedate BCPL and C aren’t zero-indexed. Algol 60 uses one-indexed arrays, and arrays in Fortran are arbitrarily indexed – they’re just a range from X to Y, and X and Y don’t even need to be positive integers.

It was actually a pretty interesting read, I recommend it if you have the time.

Re:Why Python uses 0-based indexing. It's because of slices by fdej on Wednesday October 23, 2013 @05:53PM (#6601999)
Lua, Julia, Mathematica, Matlab (ok, not so respected as a language)
Re:Why Python uses 0-based indexing. It's because of slices by atondwal on Wednesday October 23, 2013 @06:56PM (#6602330)
Mathematica is LISP, so it's really 0-based indexing, where the first element is the function you're applying. All the sugar and literals are made to hide this, but you can code like you're dealing with a real LISP.
Re:Why Python uses 0-based indexing. It's because of slices by hcarvalhoalves on Wednesday October 23, 2013 @06:20PM (#6602143)
I've found Matlab (the language) great for it's intended purpose.
Re:Why Python uses 0-based indexing. It's because of slices by yoodenvranx on Thursday October 24, 2013 @02:45AM (#6603730)
In my opinion Matlab the language sucks even for its intended use. What makes the complete package arely acceptable to work with is the environment, the documentation and the included libraries.

I did 5 years of image processing in Matlab but then switched to Python 4 years ago. One of the best choices I did in my programming career.

Re:Why Python uses 0-based indexing. It's because of slices by hcarvalhoalves on Thursday October 24, 2013 @12:20PM (#6606113)
Interesting. I make a living developing backend systems in Python, but picked up Matlab (Octave) for machine learning, and really enjoyed doing linear algebra on it. For PoCs and general experimentation I find it more amenable than messing with Numpy. Of course it isn't great for building complete systems, but for self-contained programs - which I believe is the intended purpose - it works pretty well.
Re:Why Python uses 0-based indexing. It's because of slices by calpaterson on Wednesday October 23, 2013 @06:08PM (#6602074)
I know you said "respected", but just for completeness with all the other suggestions: XML and friends.
Re:Why Python uses 0-based indexing. It's because of slices by philh on Wednesday October 23, 2013 @06:48PM (#6602283)
Perl lets you choose whatever index you like for the first element, including negative ones.
Re:Why Python uses 0-based indexing. It's because of slices by marcosdumay on Wednesday October 23, 2013 @08:40PM (#6602753)
That's because Perl does not have arrays. You've been using hashes.

In practice, Perl is not expected to be fast, so I don't think there's any difference.

Re:Why Python uses 0-based indexing. It's because of slices by neild on Wednesday October 23, 2013 @09:04PM (#6602823)
This is not correct. Perl has arrays and, as the person you are replying to said, allows the index of the first array element to be changed globally by setting a special variable. $[, if I recall correctly.
Re:Why Python uses 0-based indexing. It's because of slices by theseoafs on Wednesday October 23, 2013 @05:49PM (#6601980)
Off the top of my head, Lua uses primarily 1-based arrays. (Of course, Lua arrays are actually associative arrays, so negative numbers and 0 are also valid indices, but all the standard library functions except your arrays to start at 1.)
Re:Why Python uses 0-based indexing. It's because of slices by russellsprouts on Wednesday October 23, 2013 @05:50PM (#6601982)
Lua. However, you are allowed to use 0 based indexing as well -- but the # length operator and table.remove() will not work correctly.
Re:Why Python uses 0-based indexing. It's because of slices by aiiane on Wednesday October 23, 2013 @06:06PM (#6602062)
Also loops will potentially behave awkwardly, due to the table having shifted into dictionary-esq indexing.
Re:Why Python uses 0-based indexing. It's because of slices by anonymoushn on Wednesday October 23, 2013 @06:22PM (#6602153)
This won't be a big deal. The 0 key will be in the hash part, and the 1-#t keys will be in the array part. The language doesn't expose the distinction, so the loop-related awkwardness is limited to a tiny performance hit.
Re:Why Python uses 0-based indexing. It's because of slices by ufo on Thursday October 24, 2013 @10:39AM (#6605498)
If you try to use ipairs to iterate then it will skip the zero-th index but if you use a numeric for loop (for i=0,N do) then you should be fine.
Re:Why Python uses 0-based indexing. It's because of slices by davidw on Thursday October 24, 2013 @08:28AM (#6604814)
Erlang is 1 based. For those who know it, is this something from its Prolog heritage?
Re:Why Python uses 0-based indexing. It's because of slices by swang on Wednesday October 23, 2013 @05:53PM (#6602003)
Pascal
Re:Why Python uses 0-based indexing. It's because of slices by agumonkey on Wednesday October 23, 2013 @05:58PM (#6602029)
Does ADA `First` counts ?
Re:Why Python uses 0-based indexing. It's because of slices by eps on Wednesday October 23, 2013 @05:54PM (#6602011)
Delphi?
Re:Why Python uses 0-based indexing. It's because of slices by n00b101 on Wednesday October 23, 2013 @06:31PM (#6602207)
R?