Why Python uses zero-based indices and not one-based as in Matlab and R?
Why computer scientists count from zero?
(At) lunch break
Antje Jones: I had a list of letters:
and tried to get the second letter 'B':
Surprisingly I got a 'C'?! This is confusing! A is the 0th letter?! No one counts like that!
Mike Zero: I agree, for working with objects, 0-based makes no sense. But starting at zero is the way of thinking about time (and data-streams). We don't see the index as an label of an object (for this we use dictionaries), we see the index as a start position of a new interval: a new hour or a new block of data (reading the next 100 letters from a file). We don't 'count' from zero, we simply mean the 'first' element starts at 0.
Let's look to my morning live-stream from midnight (zero) until now:
| | | | | | | | | | | | | |
0 1 2 3 4 5 6 7 8 o'clock 9 10 11 12 now(end)
What I did between 8 and 10 o'clock?
..and then? What I did from 10 until midday
Lunch was at 12 ?
When is the meeting?
ah, OK meeting starts at 11 o'clock
Antje Jones: OK, I understand, 0-based (starting with 0) is useful when working with a time-like sequences of data. But I need to work with objects: biological samples (1st, 2nd, 3rd, etc.) and 20,000 genes for which starting with 1 is more intuitive: gene, gene,...
Mike Zero: Unfortunately, it's not possible or at least very confusing to have both 0-based and 1-based indices in the same programming language. Therefore, mathematical languages working with objects (samples) are often 1-based. More general-purpose languages (C++, Java, and Python) which frequently are used to read and process data-streams, are 0-based.
In Python, you can avoid using direct indices in for loops by using in
for element in mylist:
Alternatively, some commands offer a switch to 1-based indices
for i, element in enumerate(letter,1):
And, instead of using lists, you can handle objects by using dictionaries.
Antje Jones: Thanks a lot!
All names and characters in this dialogue are fictitious, but inspired from discussions at: