0
For the question: What is the output of this code? sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(sqs[1::4]),
why is the answer [1,25,81]
7 Respostas
+ 9
The result is 1, 25, 81 because sqs[1::4] means return every fourth element begining with index 1
+ 2
Hiral Jain [2:5:3] means return every 3rd element starting from index 2 to index 4 (excluding 5) ,so you only get 4 because 25 (the next 3rd element is at index 5)
+ 2
Hiral Jain slice in python works like this:
[start:stop:step] where:
-start is the start index
-stop is the final index-1
-step is the increment step
The slice method is a short for method.
0
1-16-64
0
If I were to print(squares[2:5:3]), why is the last index term not included i.e. answer is [4] and not [4, 25]
0
Thanks everyone :)
0
16,25,36
ans is