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]), | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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]

14th Aug 2020, 11:29 AM
Hiral Jain
Hiral Jain - avatar
7 Answers
+ 9
The result is 1, 25, 81 because sqs[1::4] means return every fourth element begining with index 1
14th Aug 2020, 11:34 AM
Gabriel Ilie
Gabriel Ilie - avatar
+ 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)
14th Aug 2020, 12:22 PM
Abhay
Abhay - avatar
+ 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.
14th Aug 2020, 12:49 PM
Gabriel Ilie
Gabriel Ilie - avatar
0
1-16-64
14th Aug 2020, 11:38 AM
El Hajbi Morad
El Hajbi Morad - avatar
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]
14th Aug 2020, 11:56 AM
Hiral Jain
Hiral Jain - avatar
0
Thanks everyone :)
14th Aug 2020, 1:04 PM
Hiral Jain
Hiral Jain - avatar
0
16,25,36 ans is
15th Jan 2023, 4:49 PM
Engr.Ayesha Liaqat
Engr.Ayesha Liaqat - avatar