Nov-19-2019, 11:38 PM
(Nov-19-2019, 10:44 PM)Drone4four Wrote: Therefore, the len() method is inclusive.I wouldn't say it this way. What would it mean for len to be exclusive, rather than inclusive? That it would actually be the length - 1? Why would you ever have a function for getting the length of something that returns that number minus 1? Don't think of len as inclusive - it doesn't do what the range or randint do, and we shouldn't try to reuse terminology that doesn't fit.
(Nov-19-2019, 10:44 PM)Drone4four Wrote: For string and list slicing it begins at the index position specified (including 0) yet is always exclusive.The way I tend to remember this is that the following function always returns true:
def f(n): return len(list(range(n))) == nWhen Python was designed, this was intentional - range(n) generates n elements. I believe the rationale was that this would be the most common use case, and so should be the easiest to learn.
(Nov-19-2019, 10:44 PM)Drone4four Wrote: For randint() parameters, it is similar to len() with counting begining where specified, inclusive of 0 (if there is a 0).Just to repeat: I wouldn't think of it as "similar to len" (as what they do is very different). While there's an argument that range() would be inclusive, I don't believe that a similar argument exists for randint. Why would you tell randint a number bigger than what you actually want, just for it to subtract from that? There's no reason for such a thing.
(Nov-19-2019, 10:44 PM)Drone4four Wrote: For range() parameters it is like slicing.This is a fair comparison. I believe the rationale for slicing was similar, in that [icopde]slicable[:n][/icode] is going to be length n. Just as with range, I believe there's a reasonable argument why it could be inclusive, but it's probably good that the two are consistent with each other. (As a side note: try evaluating [icode]list(range(10))[:20][icode] if you're not sure of what the result will be; I don't have any additional commentary on this other than finding it interesting.)
I think I've communicated my intuitions here, which are based on ~14 years of Python. Hopefully they're of use, and if they're not, hopefully someone has a correction for me :)