Why python program give output :3 to print([1,2,3,4][1:3][1]) this program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why python program give output :3 to print([1,2,3,4][1:3][1]) this program

5th Sep 2020, 9:32 AM
Henil Parakhiya
Henil Parakhiya - avatar
5 Answers
+ 7
Did u mean 3? First of all [1,2,3,4][1:3] will give the output of [2,3] which it means it taken the numbers in the 1st and 2nd index of the list (start from index 0). The the lines will become [2,3][1] which takes 1st index of numbers in the list (start from 0), which is 1
5th Sep 2020, 9:54 AM
WenHao1223
WenHao1223 - avatar
+ 2
Henil Parakhiya List= [1,2,3,4] Index [0,1,2,3] print([1,2,3,4][1:3]) #o/p:- [2,3] [1:3] select the list item starting from index 1 to 2 print([2,3][1]) #o/p:- [3]
5th Sep 2020, 9:53 AM
Ajith
Ajith - avatar
+ 2
[1:3] returns a list with element 2,3 and so [1] returns 3
5th Sep 2020, 9:53 AM
Abhay
Abhay - avatar
+ 1
Basically your program is doing this: [1, 2, 3, 4] [1, 3] = [2, 3] [2, 3] [1] = 3 print(3) Have a nice day 🧏🏼‍♀️🧏🏼‍♀️🧏🏼‍♀️
5th Sep 2020, 9:53 AM
Ariadne Rangel
Ariadne Rangel - avatar
+ 1
Thank you all for answering my question.You all guys are so kind.
5th Sep 2020, 9:57 AM
Henil Parakhiya
Henil Parakhiya - avatar