What will be the output of the following python code?explain | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What will be the output of the following python code?explain

A=[1,2,3,4,5,6,7,8,9,10,] Print(a[-1:-5])

9th Feb 2021, 11:18 AM
Code With deepak
Code With deepak - avatar
9 Answers
+ 8
That will cause an error since 'a' is not the same with A. Same with 'Print'. Syntax Errors aside, that will output an empty list [ ] A [ -1 : -5 ] A [ last_index : fifth_last_index ] In non negative index term. A [9:5] This will return an empty list since the start index is greater than the end index. https://code.sololearn.com/cO84hAw9kNje/?ref=app
9th Feb 2021, 11:28 AM
noteve
noteve - avatar
+ 3
Moreover there are many syntax errors present in your code
9th Feb 2021, 11:26 AM
Atul [Inactive]
+ 3
Atul Just to clarify. You can have negative numbers as start index, as long as it is less than the end index, otherwise it will output an empty list or empty string. For example: print(a [-5:-1] ) >> [6, 7, 8, 9]
9th Feb 2021, 11:40 AM
noteve
noteve - avatar
+ 3
Atul No need to apologise, don't mind it.👍 Just a little correction, you still give good explanations.
9th Feb 2021, 12:57 PM
noteve
noteve - avatar
+ 2
An empty list will be outputted. Because index -1 is after index -5, and the list slices only works if a[N:U:M], where "N < U and M > 0" or "N > U and M < 0", otherwise an empty list produced. The third number does not appear, so use the default number 1. That means first number should be less than the second.
9th Feb 2021, 11:27 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
Sorry all of you actually I was carrying a wrong concept in my mind . Thanks for correcting me and again sorry all of you for my blunder
9th Feb 2021, 11:54 AM
Atul [Inactive]
+ 1
a=[1,2,3,4,5,6,7,8,9,10,] print(a[1:]) Use this to print a
9th Feb 2021, 11:28 AM
Atul [Inactive]
+ 1
Atul we can start by negative index
9th Feb 2021, 11:46 AM
∆BH∆Y
∆BH∆Y - avatar
+ 1
A=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(A[-1:-5]) is this what you want
10th Feb 2021, 4:10 PM
Yusuf M Hashmi
Yusuf M Hashmi - avatar