+ 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 []

25th Apr 2022, 7:52 AM
Eric
Eric - avatar
3 ответов
+ 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/
25th Apr 2022, 8:38 AM
NonStop CODING
NonStop CODING - avatar
+ 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.
25th Apr 2022, 8:42 AM
Eric
Eric - avatar