Here is the problem statement:
return list[index:]+list[:index+3]
I know [:] represents all the elements of the list. what does this "+3" represents here?
Here is the problem statement:
return list[index:]+list[:index+3]
I know [:] represents all the elements of the list. what does this "+3" represents here?
for this line:
list[:index+3]
if index is set to 1, it is the same as
list[:4]
it's a simple sum on a variable, meaning it will read to 3 positions after your index variable
Every element from [index]
till the end plus every element from beginning up to (but not including) [index+3]
Let's have a look at an example:
>>> list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> index=2
>>> list[index:]+list[:index+3]
[3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
Here index
is 2
, thus list[index:]+list[:index+3]
is exactly the same as list[index:]+list[0:2+3]
which is list[index:]+list[0:5]
. So, Every element from [2]
till the end plus every element from beginning up to (but not including) [5]