Tuples and Dictionaries Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- Tuples Definition: An ordered, immutable sequence of elements enclosed in parentheses ((...)). Single element tuple requires a trailing comma: t = (5,) (without comma, (5) is an integer).
- Tuple Operations: Indexing, Slicing, Concatenation (+), Repetition (*), Membership (in, not in). Immutability: elements cannot be added, removed, or modified once created.
- Tuple Packing & Unpacking: Packing: t = 10, 20, 30 packs values into a tuple. Unpacking: a, b, c = t extracts elements into variables (number of variables must exactly match tuple length).
- Tuple Methods: t.count(x) (number of occurrences of x); t.index(x) (index of first occurrence of x); Built-in functions: len(t), min(t), max(t), sum(t), sorted(t) (returns a sorted list).
- Dictionary Definition: An unordered (ordered insertion from Python 3.7+), mutable collection of key-value pairs enclosed in curly braces ({key: value}).
- Dictionary Key Rules: 1. Keys must be unique (duplicate keys overwrite earlier values); 2. Keys must be of an IMMUTABLE data type (string, number, tuple); lists cannot be keys!
- Accessing & Modifying Dictionaries: Access by key: d[key] (raises KeyError if absent); Safe access: d.get(key, default) (returns default or None if absent); Adding/updating: d[key] = value; Deleting: del d[key], d.pop(key) (returns value and removes key).
- Dictionary Methods: d.keys() (view of all keys), d.values() (view of all values), d.items() (view of (key, value) tuples), d.update(d2) (merges d2 into d), d.clear() (empties dictionary).
Chapter Formulas & Equations (10 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | Single-Element Tuple Comma Requirement |
|
Parentheses without a trailing comma are treated as standard mathematical grouping operators. |
| 2 | Tuple Packing and Unpacking Equivalence |
|
Elegant Python mechanism used for parallel variable assignment and swapping variables (a, b = b, a). |
| 3 | Dictionary Key Hashability Invariant |
|
Dictionaries use hash tables for O(1) average lookup; mutable objects cannot produce permanent hash values. |
| 4 | Dictionary Lookup Time Complexity |
|
Accessing values by key in a dictionary is dramatically faster than linear searching through lists. |
| 5 | Dictionary Safe Retrieval via get() |
|
Standard defensive programming method to query dictionaries without risking runtime KeyErrors. |
| 6 | Dictionary Iteration Destructuring |
|
Idiomatic Python technique to unpack dictionary key-value pairs during loop traversal. |
| 7 | Dictionary Character Frequency Counter |
|
The standard algorithm to count word or character frequencies in strings and text documents. |
| 8 | Dictionary Merge Operation (update) |
|
Combines two dictionaries into one; in Python 3.9+ the union operator d1 | d2 performs the same merge. |
| 9 | Tuple Immutability vs Nested Mutability |
|
The tuple's reference pointers cannot change, but the mutable objects pointed to can still be modified. |
| 10 | Dictionary fromkeys() Initializer |
|
Quickly constructs a dictionary where all specified keys share an initial uniform starting value. |
CBSE Board Exam Tips & Frequent Pitfalls
- Remember: t = (5) is an integer! To make it a single-element tuple, you MUST write t = (5,).
- Dictionary keys MUST be immutable (strings, numbers, tuples). A list CANNOT be a dictionary key and will raise a TypeError: unhashable type: 'list'.
- d.get(k, 0) is the most common pattern for frequency counting questions in board practicals.
Step-by-Step Worked Board Exam Examples
Example 1: What will be the output of the following code snippet? t = (10, 20, 30) a, b, c = t a, b = b, a print('t:', t) print('a, b:', a, b)
t = (10, 20, 30) creates a tuple.
a, b, c = t unpacks: a = 10, b = 20, c = 30.
a, b = b, a swaps values: a becomes 20, b becomes 10.
Since tuples are immutable, the original tuple t remains completely unchanged: (10, 20, 30).
Example 2: Write a Python script to count the frequency of each character in a given string using a dictionary.
Initialize an empty dictionary: freq = {}.
Loop through each character ch in the string:
Use freq[ch] = freq.get(ch, 0) + 1 to increment count.
Print the resulting dictionary.
Common Mistakes to Avoid in Exam
- Using a list as a dictionary key: d = {[1, 2]: 'val'} triggers TypeError: unhashable type: 'list'.
- Writing t = (10) expecting a tuple. Without a trailing comma, Python treats it as integer 10 inside grouping parentheses.
- Accessing an absent key directly with d[k] instead of d.get(k), causing the program to crash with a KeyError.
Target Practice Questions
Q1. What is the data type of x if x = (10)?
Correct Answer: <class 'int'> (integer, because there is no trailing comma).
Q2. What does d.items() return?
Correct Answer: Returns a view object containing tuples of all key-value pairs in the dictionary: dict_items([(k1, v1), (k2, v2), ...]).
Q3. Can a tuple containing a list be modified?
Correct Answer: The tuple itself cannot be reassigned, but the elements of the mutable list inside the tuple can be modified.
Frequently Asked Questions
Q. Can a tuple be modified in Python?
No, tuples are immutable. You cannot add, delete, or change elements of a tuple once created.
Q. What happens if duplicate keys are used in a dictionary?
The last value assigned to the duplicate key overwrites any previous values for that key.
Q. What does d.pop(k) return?
Removes the key k from the dictionary and returns its corresponding value; raises a KeyError if key is not found and no default is supplied.
Q. How can you convert a list L into a tuple?
Using the tuple() constructor: t = tuple(L).