+ 3
Question about a battle question [python]
Can someone break down why the answer is [] and how the print affects this? I am not certain about the answer and am looking to refresh or study whatever made me fail this question. What will be the output of this code? a=[1,2,3,5,8,13] print(a[2:2]) answer is []
3 Respostas
+ 2
this is python list slicing
a[2:2] means starting from element location 2
ending at location 2
location No:
1 2 3 4 5
👇👇👇👇👇
a = [ 1, 2, 3, 4, 5 ]
so you are starting at location 2 and ending at location 2 that's why answer is empty.
Note: the starting location element is not included in the answer
if you write [2:3] you will get only [ 3 ] , starting location element is exclusive
read about list slicing 👇👇👇
https://www.geeksforgeeks.org/python-list-slicing/
+ 1
NonStop CODING Aaaah! Silly me, I knew I was forgetting something from a previous lesson. Now I remember the course on list slicing. Darn I as so close to a 5: on my challenge today.
Mirielle Thanks for the answer. I’ve never heard list slices explained in this way, but it makes sense now.