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.
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.
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.
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?
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?
0
drink cofee but do not copy
0