0

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

can some one please explain the concept of negative value in slicing. Also why the line print(squares[9,-1]) doesn't give 81 as output when squares[8,-1] gives 64.

6th Nov 2016, 6:04 AM
Jitesh
6 odpowiedzi
0
I don't think you need a slice to get square from this list, using only index makes what you need. about negative values - they are used to count from the end of list, so -1 is index of the last element. slice from 1 to -1 will give you a list with all squares from 1 to 64.
6th Nov 2016, 6:15 AM
Demeth
Demeth - avatar
0
Hello Demeth I am not finding square from that list. I have doubt that when I run print(squares[9,-1]) it gives me a blank list [] as answer but when I run print(squares[8,-1]) it gives [64]. so why not 81 during earlier one.
6th Nov 2016, 10:00 AM
Jitesh
0
once again, if you need square from this list, don't use slice. use index. slice[9:-1] gives empty list because in this list, 9 and -1 are the same place. if you will call slice [1:-1], it will give you a list of squares from 1 to 64, think why?
6th Nov 2016, 10:05 AM
Demeth
Demeth - avatar
0
0 once again, if you need square from this list, don't use slice. use index. slice[9:-1] gives empty list because in this list, 9 and -1 are the same place. if you will call slice [1:-1], it will give you a list of squares from 1 to 64, think why?
29th May 2022, 8:25 AM
olin josh
0
drink cofee but do not copy
29th May 2022, 8:25 AM
olin josh
0
29th May 2022, 8:27 AM
olin josh