Nov-19-2019, 10:44 PM
(This post was last modified: Nov-21-2019, 03:02 AM by Drone4four.)

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:

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:

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:

Here is the output:

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:

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.

I need you people to do two things:

- Verify that my current notes are accurate (especially the summary at the bottom)

- Share your insight that I could add to my mini guide.

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.