+ 2

Can anyone explain how this code works?

https://code.sololearn.com/cDn73dSMtXc9/?ref=app

3rd Dec 2018, 3:34 PM
Programmer Raja
Programmer Raja - avatar
5 Answers
+ 9
a=[1,2,3,45,6] print(tuple(a)[1:3]) Means select values from index 1 to index (3-1) 1 [ 2 3 ] 45 6 // values 0 [ 1 2 ] 3 4 // index hence and is 2,3
3rd Dec 2018, 3:54 PM
Rstar
Rstar - avatar
+ 4
a = [ 1,2,3,45,6] print(tuple(a)[1:3]) here in the variable name "a" , a type of list is stored which contains some numbers. In the next line it is converted into a tuple and it's elements at [1:3] indexes were called. Note:- Slicing can also be done on tuple. Note:- Slicing start from an index of 0. Note:- [start argument: stop argument-1] At index 1, 2 is present so it is printed after that stop argument is 3 that means it will stop at index 2.means on 3. Hence the result is (2,3).
5th Dec 2018, 1:05 AM
Ayush Sinha
Ayush Sinha - avatar
+ 3
a[0]=1 a[1]=2 a[2]=3 a[3]=45 a[4]=6 List slice [1:3] doesn't include 3, so you're printing a[1] and a[2].
3rd Dec 2018, 3:59 PM
Elizabeth
Elizabeth - avatar
+ 3
ПроĐČДт
4th Dec 2018, 9:23 AM
Mahmoud Kh
Mahmoud Kh - avatar
+ 2
Here you are forming tuple from the given list tuple(a) gives you (1,2,3,45,6) and then your are using slicing extracting element from this tuple starting from index 1 to index (3-1)
3rd Dec 2018, 4:03 PM
Rishi Anand
Rishi Anand - avatar