Nov-20-2019, 09:17 PM
(This post was last modified: Nov-20-2019, 09:18 PM by Drone4four.)
@ichabod801: Yes, that was a typo. Thank you for catching that. And thanks for the clarification using your lunch list example. This is enormously helpful.
@micseydel: Your sample function which returns the length of a list in a given range (
I played around some more with slicing and such. I’ve pushed my changes back up to my GitHub repo here.
Here is my take way based on what I have learned here:
One way to distinguish between len() and the integers passed into a list slice is to reflect on the following code snippet:
The final item in the list above is the 4th. But submitting '4' as a slice returns nothing. To grab the last item in a list, you need to take the total length minus 1. This distinction between len() and slicing parameters is a helpful way of recalling the difference between the two. This is clear and easy to remember.
Referring back to the code sample in my original post (here it is again):
From this code snippet, as you can see, if there is a single integer passed into the range() function, it starts at 0 and goes up to but does not include the final number in the generated list. In this way it's similar to list slicing. But when passing in parameters into randint(), the result includes any integer from the beginning parameter as well as up to (and including) the final integer at the end. This isn't quite as easy to remember. Next time I get disoriented when writing my script in the future, I'll refer back to this written distinction.
@micseydel: Your sample function which returns the length of a list in a given range (
n
) proves that it matches the function's initial n
parameter. I really like this. Thank you for sharing. I played around some more with slicing and such. I’ve pushed my changes back up to my GitHub repo here.
Here is my take way based on what I have learned here:
One way to distinguish between len() and the integers passed into a list slice is to reflect on the following code snippet:
colours5 = ["indigo", "orange", "crimson","emerald",] # To get the last index of len of list, this will not work: print(f'First attempt: {colours5[len(colours5):]}') # To get the last index of len of list... # ...you need to subtract 1 integer from the total length of the list: print(f'Second attempt:{colours5[len(colours5)-1:]}')Here is the output:
Quote:First attempt: []
Second attempt:['emerald']
The final item in the list above is the 4th. But submitting '4' as a slice returns nothing. To grab the last item in a list, you need to take the total length minus 1. This distinction between len() and slicing parameters is a helpful way of recalling the difference between the two. This is clear and easy to remember.
Referring back to the code sample in my original post (here it is again):
import random new_list = [ random.randint(0,5) for i in range(0,5) ] print(new_list)Here is the output:
Quote:[3, 4, 1, 4, 0]
From this code snippet, as you can see, if there is a single integer passed into the range() function, it starts at 0 and goes up to but does not include the final number in the generated list. In this way it's similar to list slicing. But when passing in parameters into randint(), the result includes any integer from the beginning parameter as well as up to (and including) the final integer at the end. This isn't quite as easy to remember. Next time I get disoriented when writing my script in the future, I'll refer back to this written distinction.