0

Being new to python, I'm starting to ask more and more questions. A lot of the time is understanding trying to understand 'complicated' lines of code that seem to do a lot in a single line. This has given me an better appreciations for Python as I build my own knowledge in the subject.

Background:

I've come across of a function that reverses/manipulates numbers but its difficult to understand what's happening in the IF/Else Statement. I use a few tools to better understand these statements including PythonTutor that gives me a step by step visual of what's happening in code. I've added the code right below this and the screen shots below that are from Python Tutor.

def reverse(x):
    a = str(x)
    
    if a[0] == '-':
        return int('-'+a[:0:-1])
    else:
        return int(a[::-1])
    
print(reverse(-387))
print(reverse(712))

Context:

In this screen shot below, I'm having a hard time understanding what's happening in [:0:-1]. I understand that if there's a negative in the integer, then continue w/ this statement but why would it be starting at the :0 in this statement? Aren't we trying to take the last int which is -1 and put it to the beginning?

My question is shouldn't it be [:-1:0] since we want to capture the last int and place it first?

enter image description here

I have the same type of question, which is confusing me, related the Else portion of the IF statement (below). The portion [::-1] shows that there's no need to capture the 0 in it, Why is this? Don't we still need the 0 index because we still need to reverse a positive integer, correct?

enter image description here

I guess this can be a 2-part question. Would love to understand this better and if there's a version of this that may not been a IF/Else Statement, that may also help me understand this better.

  • 1
    You don't seem to understand Python ranges. When used as a slice index, the general form is `[start:end:step]`. The sequence begins at `start`, and stops *before* `end` (i.e., `end` is excluded`). The `step` tells it how much to advance the index each time. If `start` is absent, it is `0` (for a positive `step`) or `len(seq) - 1` for a negative `step`. If `end` is absent, it is `len(seq)` (for a positive `step`) or the position before `0` (like `-1`, but without the special interpretation for negative values) (for a negative step). – Tom Karzes Oct 10 '21 at 17:07
  • 1
    So `[::-1]` reverses the *entire* sequence. `[:0:-1]` skips the first item and reverses the rest. Your proposed `[:-1:0]` is invalid, since a `step` value of `0` would just repeat infinitely. And indeed, if you attempt a step size of `0`, you will receive the appropriate error. – Tom Karzes Oct 10 '21 at 17:09
  • The above link contains a good answer to this duplicate question. – Tom Karzes Oct 10 '21 at 17:11
  • Seems to me a duplicate question - as @TomKarzes pointed out. – Daniel Hao Oct 10 '21 at 17:47
  • 1
    @WilliamZebrowski - 1) avoid using the built-in method `reverse` (changed to my_reverse, eg); 2) include the code next time, not the image. – Daniel Hao Oct 10 '21 at 17:58

0 Answers0