Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 88 of 102
Marks: +1, -0
(a) Given is a Python list declaration :
Listofnames=["Aman","Ankit","Ashish","Rajan","Rajat"]
Write the output of :
print (Listofnames [-1:-4:-1])
[1]
(b) Consider the following tuple declaration :
tup1=(10,20,30,(10,20,30),40)
Write the output of :
print(tup1.index(20))
[1]
[CBSE 2023 Set 4]
Solution:  
(a) The slice [-1:-4:-1] starts at index -1, moves backward with step -1 and stops before index -4, so it takes indices -1, -2, -3: 'Rajat', 'Rajan', 'Ashish'.
['Rajat', 'Rajan', 'Ashish']
(b) index(20) returns the position of the first occurrence of 20 among the elements of the tuple. 20 is at index 1 (the 20 inside the nested tuple is not an element of tup1).
1
© examsnet.com
Advertisements
Go to Question: