0

Slicing Doubt : why does the 2nd print statement result in blank?

animals =["cat", "dog", "bird", "cow"] print(animals[-2:4]) print(animals[-2:-4]) # why is it blank?

6th Aug 2023, 11:59 AM
Dayya
Dayya - avatar
2 Respuestas
+ 4
Ray , Negative slicing numbers are arranged like this ----> -4,-3,-2,-1[for your code] So [-2:-4] Here first one denotes starting... Second one denotes end(which is exclusive)... Always it works from left to right... If you arrange like [-2:-4] means after -2 it goes to -1... So try like this [-4:-2]----> here it goes from -4 to -3 and stops since -2 is exclusive... Here it is implemented, https://code.sololearn.com/cpn59X26qp7u/?ref=app
6th Aug 2023, 12:10 PM
Riya
Riya - avatar
+ 5
Because the end item index is smaller than the first. That means -4 points on the first item and -2 on the third. [start:end]
6th Aug 2023, 12:13 PM
JaScript
JaScript - avatar