Q.Write a program to find the largest and the second largest elements in a given list of elements.
Concept understanding — List Operations
A shopping list on paper is more than the items written on it — it is the things you can do with it: read the third entry, cross one out, add another at the bottom, check whether "milk" is already there. A Python list is the program's version of that paper: an ordered collection that can hold many values under one name, written in square brackets. List operations are the standard actions the language gives you for working with the whole collection or any part of it.
marks = [78, 92, 65, 88, 71]
The core operations every student must command:
- Indexing — fetch one element by its position: marks[0] is 78. Counting starts at zero, so the last of five items is marks[4]. Negative indexes count from the end: marks[-1] is the last element, no matter how long the list grows.
- Slicing — fetch a sub-list: marks[1:4] gives the elements at positions 1, 2 and 3. The start is included, the stop is excluded. Leaving an end open means "to the edge": marks[:3] is the first three, marks[2:] is everything from position 2 onward.
- Concatenation — joining two lists with the plus sign produces a new, longer list.
- Repetition — a list times an integer produces repeated copies, just as with strings: [0] * 5 builds a list of five zeros, a handy way to prepare storage.
- Membership — the in and not in tests answer yes or no: 92 in marks is True. No loop needed.
- Traversal — a for loop visits every element in order, which is how totals, searches and printouts are written.
- Length — len(marks) reports how many elements the list currently holds.
Unlike a string, a list is mutable — it can be changed in place. Assigning marks[2] = 70 genuinely replaces the third element; nothing similar is allowed on a string.
Asking for an index that does not exist — marks[10] on a five-item list — crashes with IndexError. A slice, oddly, forgives the same mistake: marks[10:20] just returns an empty list. Examiners love this contrast.
These operations matter because the list is the workhorse container of the whole course: marks of a class, items in a bill, rows read from a file all arrive as lists, and every later technique — searching, counting frequencies, computing averages — is built by combining exactly these few operations.
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.