May-19-2023, 04:21 PM
(This post was last modified: May-19-2023, 04:21 PM by deanhystad.)
Slicing starts at the start number and ends before the end number. I think this is because indexing starts at 0.
range() use the same protocol.
things = "abcdefg" print(things[:3]) # Print the first 3 letters.If the slice contained the ending value you would write this:
things = "abcdefg" print(things[:2]) # Print the first 3 letters.Hmmm. I want three values, but I need to ask for 2?
range() use the same protocol.
for i in range(3): # I want to iterate 3 times print(i)
Output:0
1
2
Similar to slicing, range would look odd if the range included the end number.for i in range(2): # I want to iterate 3 times. Why do I need to enter 2? print(i)When you start with zero, the number in range(number) equals the number of iterations. You can set the start number to any value, but by a huge margin most range(start, end, increment) skip setting the start and increment.