10 / 18 Stride Length


#1

Trying to figure out how to get it to come to zero. the second list is mine and I added a comment. If I didnt want to leave the start : finish blank, how would I get it to come to 0. putting it to zero didnt get it to reach to zero, or -1, or -10.


#2

Hello :slight_smile:

Why it didn’t get to 0? Because the second “parameter” of slice is an exclusive index, it’s the first index that will be excluded from the resulting list. And range works in the same way:

range(3, 7):
=> [3, 4, 5, 6]

You see? It does not include 7.


So why -1 or -10 did not work?

In Python negative indices have a special meaning. Index -1 refers to the last index, index -2 to the second to last etc.

to_one_hundred[100:-15:-2]
=> [100, 98, 96, 94, 92, 90, 88]

how would I get it to come to 0

You can use None if the blank space really bothers you.

to_one_hundred[100:None:-10]
=> [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]

I would like to emphasize one point. Do not treat slice parameters as values, they are indices. Codecademy exercise uses list from 0 to 100 so it might be misleading. Here is an example:

test_list = range(0, 60, 3)

> test_list
=> [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57]

> test_list[4:7]

What is the result? [12, 15, 18]. Because 12 is the fifth element of the test_list


#3

Thanks for the help!


#4

Your’re welcome :slight_smile:


#5

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