About lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

About lists

Can someone explain both? A = [1,2,3,4,5] Print [-1:-3:-1] And Print[:]

22nd Jun 2020, 8:15 AM
sunitha
sunitha - avatar
6 Answers
+ 4
Print(a[:]) Will return all value as a list have because it indicates from first to last And for print(a[-1:-3:-1]) Assume it as a[-1:-(3-1)] And in reverse order last index is indicated with -1
22nd Jun 2020, 9:02 AM
Ayush Kumar
Ayush Kumar - avatar
+ 3
' : ' is slice operator,it will take two arguments and one more optional arguments. The two arguments are start and end . Here start is included and end is excluded. print(A[:]) here no arguments given it start at 0 and ends at len(A). Third one option argument is jump. A[-1:-3:-1] Start -1 End -3 Jump -1
22nd Jun 2020, 9:07 AM
uday kiran
uday kiran - avatar
+ 2
Got it thank you 😊
22nd Jun 2020, 10:11 AM
sunitha
sunitha - avatar
+ 2
Devika Krishnan i was asking how it works. Its not defined.
23rd Jun 2020, 5:57 AM
sunitha
sunitha - avatar
+ 2
In list A = [1,2,3,4,5] We will count, 1 as 0 2 as 1 3 as 2 4 as 3 And so on... If you print [:] in this way, then the output would be [1,2,3,4,5]
23rd Jun 2020, 2:29 PM
DeepesHackeR
DeepesHackeR - avatar
+ 1
but this code will show u an error Print[-1:-3:-1] NameError: name 'Print' is not defined
23rd Jun 2020, 5:48 AM
Devika Krishnan
Devika Krishnan - avatar