Python Forum
"Up to but not including" (My personal guide on slicing & indexing)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Up to but not including" (My personal guide on slicing & indexing)
#3
(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))) == n
When 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 :)
Reply


Messages In This Thread
RE: "Up to but not including" (My personal guide on slicing & indexing) - by micseydel - Nov-19-2019, 11:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Function to count words in a list up to and including Sam Oldman45 15 6,755 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Complete Guide to Shodan Calli 4 2,356 Jul-18-2022, 04:29 PM
Last Post: Calli
  Including data files in a package ChrisOfBristol 4 2,604 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,074 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,948 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  slicing and indexing a list example leodavinci1990 4 2,387 Oct-12-2020, 06:39 AM
Last Post: bowlofred
  Need to access Windows Machine or Personal Certificate Store and send to web app seswho 0 1,663 Sep-14-2020, 04:57 PM
Last Post: seswho
  New User online Guide NAP1947 1 1,577 Sep-06-2020, 04:36 PM
Last Post: Larz60+
  Including modules in Python using sys.path.append JoeDainton123 1 2,946 Aug-24-2020, 04:51 AM
Last Post: millpond
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 1,911 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020