How does it print 9, 25, 49, 81 when the slice is between 2 and 2?


#1



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


How does it print 9, 25, 49, 81 when the slice is between 2 and 2?


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

print l[2:9:2]


#2

No, the slice is between 2 and 9. That last 2 means every 2 index.
Hence why it skips 1 index.


#3

Which lesson is this ?


#4

I got it. Thanks. I misread [2:9:2]. I got the last 2 and the 9 mixed up. I thought the stride was 9 not the 2.


#5

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