+ 1

Can anyone pls expain how this code output this answer?

Input: squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[2:8:3] Output: [4, 25]

9th Jan 2022, 3:06 AM
Emmanuel
Emmanuel - avatar
1 Resposta
+ 3
Printing squares in range 2 to 8 with increment of 3. So it prints: squares[2] is 4, then adds 3 so it prints squares[5] is 25, then tries to add 3 again but that is more than range[2:8] remembering that range is up to but not including the second value. edit: this wasn't 100% correct. it isn't "range", it is a "list slice" (behaves almost exactly the same as "in range".
9th Jan 2022, 3:17 AM
HungryTradie
HungryTradie - avatar