Q.(a) Given is a Python list declaration: Listofnames=["Aman", "Ankit", "Ashish", "Rajan", "Rajat"] Write the output of: print(Listofnames[-1:-4:-1])
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The first prints a slice of the list in reverse from the last element, stopping before index -4; the second finds the position of the first occurrence of 20 in the tuple.
Understanding Python List and Tuple Operations
Python treats lists and tuples as ordered collections, and both support powerful indexing and slicing mechanisms. The key to solving these problems lies in understanding how negative indices work and how built-in methods behave.
Part (a): List Slicing with Negative Indices
The list Listofnames contains five strings. When you write Listofnames[-1:-4:-1], you're asking Python to slice the list using three parameters: start, stop, and step.
Negative indices count backward from the end. In this list, index -1 refers to "Rajat" (the last element), -2 to "Rajan", -3 to "Ashish", -4 to "Ankit", and -5 to "Aman". The slice notation [-1:-4:-1] means:
- Start at index -1 ("Rajat")
- Move backward (step of -1)
- Stop before reaching index -4 ("Ankit")
So Python begins at "Rajat", steps backward through "Rajan" and "Ashish", and stops just before "Ankit". The slice captures elements at positions -1, -2, and -3.
The stop index in a slice is always exclusive — the element at that position is never included in the result, regardless of direction.
Output: ['Rajat', 'Rajan', 'Ashish']
Part (b): Finding an Element's Index in a Tuple
The tuple tup1 contains five elements: 10, 20, 30, (10,20,30), 40. Notice that the fourth element is itself a nested tuple. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.