Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

I actually prefer the 0 indexing of python. Systems code (c/c++ etc.) already uses 0 based indexing. So it is nice that when you do data science the convention stays the same.


sort by: page size:

can you give me an example? I did all sorts of numerical indexing programming, from fortran, matlab, python/numpy and R, and I am definitely more comfortable with 0 based indexing.

No! Python 0 based indexing everywhere.

Python supports -1 based indexing, so clearly that's the superior convention.

Zero based indexing? I find it not too bad to switch between languages that use zero & one based indexing.

This is something I don't fundamentally understand. As someone who works a lot with MATLAB I'm very used to and like 1-based indexing. But when I use C or Python, 0-based indexing is not something I complain about or hold against the language. It's just the way things are.

Maybe if you don't think of it in terms of a different index basis and instead you think of it as indexing vs. offsets then it becomes easier to switch between the two?


I had been comfortably programming in C and python for years and found 0-based indexing to be perfectly sensible. For the last couple of years I've been programming a lot in Julia. Initially I was horrified by the idea of indexing from 1 but I got the hang of it remarkably fast. Now I feel it's totally natural either way, depending what language I'm in.

I think it's only ever been a matter of preference as you say. And Julia was otherwise such a pleasure to write code in that it was easy for me to pay the small price to learn that.


Honestly 0-based indexing is one of the most annoying things to teach. In sciences, people really don't care about programming arcana: I have never found anyone who found 0-based indexing natural or intuitive.

In fact, much of the vehemence in favor of it seem to be more about programmer shibboleths, to make some people feel they are in a special 'in-the-know' club (certainly if the r/programmerhumour reddit is anything to go by).

All I can do for scientists is tell them the advantage of learning a programming language outweighs the weirdness.


To tell you the truth, I don’t really care about 0 or 1 based indexing, there’s not much difference when I’m coding.

At the same time if you look at TIOBE, 0 based indexing won.

The biggest problem for me was porting numerical code from Ruby: the syntax is so similar to Julia, that I didn’t have much to do, but porting 0 based code to 1 based was the biggest work actually.

I’m sure it’s painful to port code from R or Marlab if it’s 0 based, but that will need to happen anyways.


Yeah, I don't get the love for 0 base, unless you're doing C level stuff, which is the real reason for 0 based indexing.

Let’s not ignore the giant elephant in the room: 1-based indexing. I don’t particularly care since I use R and Python but Java, C, C/C++, C# all used 0-based indexing. It’s truly a bizarre choice Julia made there.

As someone coming from a physical science rather than CS background, I routinely find 0-based indexing to be "frustrating and a mental tax" compared to 1-based

That part has always annoyed me.

But then Python beat me into submission with its seductive collection comprehension... Now I see it as a standard convention, like indexing should start with 0.


In my experience, 0-based indexing is way more common in mathematics. Maybe it depends on the field you're working in. Off the top of my head, dimensions are numbered from zero in relativity and particle physics, Fourier series only make sense with 0-based indexing, coefficients of polynomials are numbered from zero, Taylor series, indexing by taking the modulo ...

A little bit about 0-based indexing on the practical side from Guido van Rossum: http://python-history.blogspot.hu/2013/10/why-python-uses-0-...

The truth is that everyone who programs seriously at any level can deal with both 0- and 1- based indexing. It's just not that hard to get used to.

And science has lots of 1- based indexing: Matlab, Fortran, R, most math...


Historically 0 based is for low level languages. 0 based makes sense for C.

My issue is higher level languages Python, Java, C# there is plenty of things that just are complicated and doing arrays off 0 is one of them. Doing data science or statistics just makes it obvious that you have two different sets on numbers. 1 doesn't mean the same thing in every instance in your programming and the functional programming side of me hates that.


These are excellent points. I find that many programmers will assume that zero based indexing is better because that's what was drilled into them until they stopped making the mistake and authorities in computer science (like Dijkstra) said that it's better.

Having used both 1-based and 0-based. Even started on 1-based with Matlab. 0-based indexing is my preferred!

> or any other kind of offset that you so desire

This is too often the attitude of Julia people. But zero-based indexing is not just some arbitrary offset. Programmers just like it. You say that many things are more intuitive with one-based indexing, but many things are less intuitive too. I don't think python ever would have become popular without zero-based indexing.

The thing is, we really don't know why people prefer certain types of language. All we know is that they do. Computer languages can't evolve gradually like natural languages, but if they could I really don't think we'd go to one-based indexing.

next

Legal | privacy