+ 2
Can anyone explain how this code works?
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
+ 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).
+ 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].
+ 3
ĐŃĐžĐČĐ”Ń
+ 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)