7. List Slicing Syntax


#1



Sorry to bother, index starts with 0...


https://www.codecademy.com/courses/python-beginner-en-KAgt5/2/1?curriculum_id=4f89dab3d788890003000096


Hi. Quick question. In this exercise instructions say - [start:end:stride]
Where start describes where the slice starts (inclusive), end is where it ends (exclusive), and stride describes the space between items in the sliced list. For example, a stride of 2 would select every other item from the original list to place in the sliced list.
But it doesn't include 1st number in the print statement which should be inclusive.


l = [i ** 2 for i in range(1, 11)]
# Should be [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

print l[2:11:2]

print l[2:11:1]

print l[2:11:3]

Prints out:
[9, 25, 49, 81]
[9, 16, 25, 36, 49, 64, 81, 100]
[9, 36, 81]
None

Doesn't look like it starts with 2. It would be somewhat ok if it would start counting from 2 (i.e stride 1 > start with 3, stride 2 > start with 4), but it also not the case. It skips to the next number in the list without factoring in the stride and excluding the first number in request it is supposed to include.

Thank you for clarification.


#2

1, 4, 9, 16, 25, 36, 49, 64, 81, 100
0  1  2   3   4   5   6   7   8    9  ---ELEMENT-count
      |
      +-------------------------------2 inclusive !!!!

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.