0

UPDATE: I got code from sololearn.com (freemium site to teach Python). And I ran code on my own Python 3.

I don't understand this output

sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(sqs[7:5:-1])
[49, 36]

Is the interpreter skipping 0th element, i.e. 0 and reading list as [1, 4, 9, 16, 25, 36, 49, 64, 81], and then printing the 7th element, i.e 49?

How does it then print 36? Does it go forward and read [64,81], then read backwards [64,49,36], hence printing 5th element, i.e. 36?

If this is correct, then explain following output

>>> print(sqs[7:4:-1])
[49, 36, 25]

I can understand that python skips the 0th element, counts till 7 and prints 49. But now I specified a gap of 4. So shouldn't the interpreter read [64,81], then go in reverse, i.e. [64,49], and print 49? And how did it print 25?

Marium
  • 245
  • 3
  • 10
  • 5
    The notation means start at the seventh index `7`, until the fifth (not including it) `5` and decrease 1 each time (step `-1`). – Dani Mesejo Jan 31 '19 at 19:30
  • 2
    sqs[7:4:-1] means it starts with the 7th element and read backward with -1, e.g. reads 6th, 5th, and then stops reading because it's now on the 4th element. – Tim Jan 31 '19 at 19:32
  • 2
    49 is at index 7, 36 is at index 6. Remember, the first index is 0, not 1. – Carcigenicate Jan 31 '19 at 19:32
  • @pault I saw the duplicate question, it did not help me, hence this post. – Marium Jan 31 '19 at 20:00

2 Answers2

2

See the defination for range() method in Python docs.

range(start, stop, step)

Here, start specifies the value from where it will start, stop specifies the value before which it has to stop i.e stop-1. step denotes the jump. Now, if the parameters are something like range(2,5,1), then it will start from 2, then make a jump of 1 i.e add 1 to 2. Now, again it will add 1 to 3, and it becomes 4. Now, 4 is equal to 5-1. Hence it stops.

List slicing is something similar. Now, you have something like this:

sqs[7:5:-1]  

This is similar to range but here start is 7, stop is 5 and step size is -1. Hence it generates a descending order list. Now the values it generates are :

[7, 6]

5 is not counted because, stop =5 and we have to consider only upto stop-1, i.e 5-(-1) = 6.
This is how it works.

taurus05
  • 2,491
  • 15
  • 28
  • I will describe slicing after explaining range method. It will be much easier to understand this way. – taurus05 Jan 31 '19 at 19:32
  • FWIW although the answer started with an unrelated explanation, I think it was a good illustration using `range()` to help understand. Would probably be better if you linked the slice documentation to avoid confusion. – r.ook Jan 31 '19 at 19:41
  • Yes, it is really helpful to make clear references using comparisions and analogy. – taurus05 Jan 31 '19 at 19:44
2

The slice [i:j:k] says take the elements starting at index i, stepping by k, until j is reached. So [7,4,-1] get indices 7, 6, and 5. See doc.

John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • I didn't see it that way. Because we are going backwards it is 7, 6, 5 (but the 5th element is not printed, it is merely a border). If I specified `print(sqs[7:3:-1])`, it would print 7th, 6th, 5th, 4th elements. – Marium Jan 31 '19 at 20:00