The second revision tour consolidates the compound data types and control structures of Python that are essential for the Class 12 syllabus. While the first tour covered the language's basics, this tour focuses on strings, lists, tuples, dictionaries and sets, together with the conditional and looping statements that drive programs. These collections appear in nearly every chapter that follows, from stacks and queues implemented with lists to the processing of data tables represented as lists of dictionaries.
A sequence in Python is an ordered collection of elements supporting indexing, slicing and iteration. Strings, lists and tuples are all sequences. Dictionaries and sets, while also collections, differ: dictionaries map keys to values, and sets store unordered unique elements. Control structures, the if family and the while and for loops, operate on these collections to make decisions and repeat work.
This chapter revises each type with its key methods and operations, covers the two loop statements with break, continue and the loop-else clause, and includes the sorted, reversed, min, max, sum and enumerate built-in functions. Accurate recall of method names and their exact behaviour is essential, since questions in the examination frequently ask for the output of short programs using these operations.
A string is an immutable sequence of characters enclosed in single, double or triple quotes. Strings support indexing from 0, negative indexing from the end, slicing with [start:stop:step], and a rich set of methods.
word = "Python"
print(word[0]) # P
print(word[-1]) # n
print(word[1:4]) # yth
print(word[::-1]) # nohtyP
text = " Hello World "
print(text.strip()) # Hello World
parts = "A,B,C".split(",")
print(parts) # ['A', 'B', 'C']
print("-".join(parts)) # A-B-C
print(text.lower().strip()) # hello world
Strings cannot be modified in place. Expressions like word[0] = "J" raise a TypeError. To change a string, a new string is built, usually by slicing.
word = "Python"
new_word = "J" + word[1:]
print(new_word) # Jython
A list is a mutable, ordered sequence of elements enclosed in square brackets. Elements can be of mixed types, and a list can contain other collections. Lists support indexing, slicing, concatenation and repetition, and provide methods for modifying content.
lst = [10, 20, 30, 40]
lst.append(50) # add at the end
lst.insert(1, 15) # insert at index 1
lst.remove(30) # remove first occurrence of 30
print(lst) # [10, 15, 20, 40, 50]
popped = lst.pop() # remove and return last element
print(popped) # 50
nums = [3, 1, 4, 1, 5]
nums.sort()
print(nums) # [1, 1, 3, 4, 5]
print(nums.count(1)) # 2
Slicing returns a new list. lst[start:stop] includes start, excludes stop; lst[:] copies the whole list; lst[::-1] reverses it.
lst = [0, 1, 2, 3, 4, 5]
print(lst[1:4]) # [1, 2, 3]
print(lst[::2]) # [0, 2, 4]
A tuple is an immutable sequence enclosed in parentheses. Tuples are faster and protect data from accidental modification. A single-element tuple needs a trailing comma, like (5,). Since tuples are immutable, they have no append, insert or remove methods; they support indexing, slicing, concatenation and the count() and index() methods.
t = (10, 20, 30)
print(t[1]) # 20
print(t[:2]) # (10, 20)
a, b, c = t # tuple unpacking
print(a, b, c) # 10 20 30
Tuple unpacking assigns each element to a variable in order, a common Python idiom.
A dictionary stores key-value pairs in braces. Keys must be immutable (strings, numbers, tuples) and unique; values may be of any type. Dictionaries are mutable and unordered in older Python, though modern Python preserves insertion order.
student = {"roll": 1, "name": "Aarav", "marks": 92}
print(student["name"]) # Aarav
student["marks"] = 95 # update value
student["city"] = "Delhi" # add new key
print(student.get("grade", "NA")) # NA (default)
del student["city"]
print(len(student)) # 3
for key, value in student.items():
print(key, ":", value)
A set is an unordered collection of unique elements enclosed in braces. Duplicate elements are automatically removed. Sets support mathematical operations: union (|), intersection (&), difference (-) and symmetric difference (^), plus methods like add(), remove() and discard().
a = {1, 2, 3, 3}
print(a) # {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5}
print(a & b) # {3}
print(a - b) # {1, 2}
Since sets are unordered, they do not support indexing or slicing.
The if, if-else and if-elif-else statements make decisions. Only the block of the first true condition executes; the else block is optional and runs when no condition holds.
marks = 85
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
else:
grade = "C"
print("Grade:", grade) # Grade: B
The for loop iterates over any sequence or iterable, most commonly range(), a list, a string, a dictionary's keys, or the enumerate() function.
for i in range(5):
print(i, end=" ") # 0 1 2 3 4
for index, value in enumerate(["a", "b", "c"]):
print(index, value) # 0 a 1 b 2 c
The while loop repeats while its condition is true. The loop control variable must be updated inside the body to avoid infinite loops.
total, n = 0, 1
while n <= 5:
total += n
n += 1
print(total) # 15
break exits the loop immediately. continue skips the rest of the current iteration. The else clause of a loop runs only when the loop finishes normally without break.
for n in range(2, 20):
if n % 3 == 0:
print("First multiple of 3:", n)
break
else:
print("No multiple of 3")
Python provides several useful built-ins. len() returns the number of elements, sum() adds numeric elements, min() and max() find extremes, sorted() returns a new sorted list, reversed() returns a reversed iterator, and enumerate() pairs elements with their indices.
marks = [85, 92, 78, 95]
print(sum(marks)) # 350
print(max(marks)) # 95
print(min(marks)) # 78
print(sorted(marks)) # [78, 85, 92, 95]
print(list(reversed(marks))) # [95, 78, 92, 85]
| Type | Mutable? | Example | Key Methods |
|---|---|---|---|
| str | No | "Python" | upper, split, join, find |
| list | Yes | [1, 2, 3] | append, insert, pop, sort |
| tuple | No | (1, 2, 3) | count, index |
| dict | Yes | {"a": 1} | keys, values, get, items |
| set | Yes | {1, 2, 3} | add, remove, union |
| Statement | Effect |
|---|---|
| break | Terminate the loop immediately |
| continue | Skip the rest of the current iteration |
| else (with loop) | Runs only if loop finishes without break |
The second revision tour completed the core toolkit of Python programming. Strings, lists, tuples, dictionaries and sets provide every common way to store and organise data, each with its own mutability, ordering and operations. Control structures give programs decision-making and repetition, with break, continue and the loop-else clause providing precise control. Built-in functions such as sum, max, min, sorted and enumerate make working with these collections concise and expressive. This knowledge is the foundation for the remaining chapters: functions and recursion build directly on these types, and the data structures of stacks and queues rely on lists. The next chapter examines functions, the mechanism by which programs are organised into reusable, manageable units.