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)
#1
I’m writing a mini guide for myself for future reference on indexing strings and lists and how they compare to parameters that are passed into randint() and range().

I need you people to do two things:
  1. Verify that my current notes are accurate (especially the summary at the bottom)
  2. Share your insight that I could add to my mini guide.
Here is my guide in a Jupyter Notebook hosted on my GitHub repo. It’s kind of long. Here is the important part that I need you call to evaluate and comment on:

What follows is a list comprehension operation which is a demonstration of both range() and randint() which take in identical numerical parameters but produces different behaviour. The randint() starts at '0' and goes up to and includes the final number - - so can generate from a spectrum of 6 potential random integers: 0,1,2,3,4,5. However range() produces a total of strictly 5 items, not 6. See here:

import random

new_list = [ random.randint(0,5) for i in range(0,5) ]
print(new_list)
Here is some output:

Quote:[1, 4, 0, 5, 3]

As shown above, passing in the same '0,5' (for randint() and range()) creates a list of a total of 5 items but 6 potential integers - - 0,1,2,3,4,5.

Now check out this demo of len() method on list/strings:

colours = ["indigo", "orange", "crimson", "emerald",]
print(f"The length of the colours string is {len(colours)}. \
 But the 4th item in the lists is pulled using [3]: {colours[3]} \n")
 
quick_string = "Bob"
print(f'Here is a new string I just created: "{quick_string}". \
 The length of this string is {len(quick_string)} \
  but to pull the 3rd character, you use [:2] which results in this: "{quick_string[:2]}"')

Here is the output:

Quote:The length of the colours string is 4.
But the 4th item in the lists is pulled using [3]: emerald
Here is a new string I just created: "Bob".
The length of this string is 3 but to pull the
3rd character, you use [:2] which results in this: "b"

Therefore, the len() method is inclusive. len() starts at the 0 index position but the counting starts at '1'. This is true for both strings and lists.

To summarize for my future reference, here is my "Up to but not including" legend:
  • For strings and lists, len() begins counting at the 0 index position but begins counting as 1 and is always inclusive.
  • For string and list slicing it begins at the index position specified (including 0) yet is always exclusive.
  • For randint() parameters, it is similar to len() with counting begining where specified, inclusive of 0 (if there is a 0).
  • For range() parameters it is like slicing.

Is this all accurate? What have I got wrong? What have I got right? Is there anything that you'd like to correct? How might you people clarify what I am trying to say here?

In terms of research so far, I’ve used more than one Udemy course on Python like Jose Portilla’s “Complete Python 3 Masterclass Journey” as well as Colt Steele’s "The Modern Python 3 Bootcamp". I've used a Pynative guide called “Python range() Function Explained with Examples” as well as a Stackoverflow question/answer thread titled “Difference between random randint vs randrange” helped really confuse me as well.
Reply


Messages In This Thread
"Up to but not including" (My personal guide on slicing & indexing) - by Drone4four - Nov-19-2019, 10:44 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,606 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Complete Guide to Shodan Calli 4 2,286 Jul-18-2022, 04:29 PM
Last Post: Calli
  Including data files in a package ChrisOfBristol 4 2,552 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,915 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,927 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  slicing and indexing a list example leodavinci1990 4 2,353 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,644 Sep-14-2020, 04:57 PM
Last Post: seswho
  New User online Guide NAP1947 1 1,551 Sep-06-2020, 04:36 PM
Last Post: Larz60+
  Including modules in Python using sys.path.append JoeDainton123 1 2,912 Aug-24-2020, 04:51 AM
Last Post: millpond
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 1,891 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