0

I found this previously answered here, Best way to loop over a python string backwards

I need to use OP's original idea, but I don't quite understand the meaning of the stop argument in the range function. What does 0-1 mean? From 0-1? Wouldn't that be the step?

Community
  • 1
  • 1

2 Answers2

2

Why can't you use reversed as accepted in the previously answered question?

However, to answer your original question:

The 0-1 is actually just a number 0-1, which is equal to -1

The documentation for range says the following: range(start[, end[, step]]). His call looks like the following: range(len(string)-1, 0-1, -1)

start = len(string)-1, which is last letter of the string.

end = 0-1 which is equal to -1, so the last letter being handled is 0. Remember that range(0, 3) will actually give 0, 1, 2 and stop right before the 3. Same goes for negative numbers, it will stop right before -1, so it will stop at 0.

step = -1. Step stands for how many numbers to step over at once. -1 means it will lower i by one every time.

  • Thank you. Because assignment says specifically not to use reveresed. If end was 0-1 to mean -1, why not just type -1? –  Jan 27 '13 at 18:12
  • 2
    @algebr That question, I am unable to answer. He probably saw it somewhere and just copy-pasted without thinking of the meaning. It doesn't have any impact on the execution of the program. `0-1` **is the same as** `-1`, so you should indeed type `-1` instead. –  Jan 27 '13 at 18:14
1

The 0-1 is -1, it can be written either way:

>>> -1
-1
>>> 0-1
-1

Let's try a string length of 10, stop of 0-1 ad step of -1:

>>> range(10, 0-1, -1)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

you'll see the same result with -1 instead of 0-1:

>>> range(10, -1, -1)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

The step of -1 causes range to count backward, let's try a step of 1:

>>> range(10, 0-1, 1)
[]

When in doubt, shell it out

Chris Montanaro
  • 16,948
  • 4
  • 20
  • 29