+ 2
About lists
Can someone explain both? A = [1,2,3,4,5] Print [-1:-3:-1] And Print[:]
6 odpowiedzi
+ 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
+ 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
+ 2
Got it thank you 😊
+ 2
Devika Krishnan i was asking how it works. Its not defined.
+ 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]
+ 1
but this code will show u an error
Print[-1:-3:-1]
NameError: name 'Print' is not defined