0

Would someone be able to explain in simple English why this happens when I try to index the below... I thought I should be getting back 45, 49, 52, 55 as my answer? I'm sure I am missing something here...

times = (43, 45, 49, 52, 55)
print(times[1:4])
(45, 49, 52)
vaultah
  • 44,105
  • 12
  • 114
  • 143
Ali
  • 837
  • 2
  • 12
  • 18
  • I have a general understanding of indexing, but why doesn't the 55 show up? When I do print(times[0:4]) it returns 43,45,49,52 which is what I would expect. But when I start from 1 in the indexing it doesn't seem to follow the same logic? – Ali Jun 19 '16 at 08:22
  • Copied from the top answer: `a[start:end] # items start through end-1` – vaultah Jun 19 '16 at 08:24
  • Thanks, I can see what you mean; however the logic behind it though I don't understand. Why not give me 45,49,52,55? – Ali Jun 19 '16 at 08:34
  • Because `start` is 1 and `end-1` is 3. – vaultah Jun 19 '16 at 08:36
  • Thanks, but why the end-1 - is there a logical reason why it does this? – Ali Jun 19 '16 at 08:38
  • Because Python developers defined slices that way? I don't know. But for what it's worth, the behavior is consistent e.g `range(1, 4)` does not include 4. – vaultah Jun 19 '16 at 08:41
  • Ok, I'll accept it for what it is (I just wish I knew the logic why they do this). Thanks vaultah appreciate it :-) – Ali Jun 19 '16 at 08:42
  • @Alvis you may considering referring to http://programmers.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm – Tadhg McDonald-Jensen Jun 19 '16 at 19:24

0 Answers0