Python Basics: Accumulating Lists, Tuples, and Unpacking (Last-Minute Review)
Chapter 1: Introduction
Accumulating a list: loop through WordDict to build another list of palindromes (words that read the same backward and forward).
Palindrome check: define is_palindrome(word) that returns True if the reversed word equals the word.
Reversing: reverseword(word) reverses the string; ispalindrome uses that result.
Accumulator pattern: before the loop, count = 0; inside the loop, if is_palindrome(word): count += 1. After the loop, count is the total number of palindromes.
Build a palindrome list: palindromes = []; if is_palindrome(word): palindromes.append(word).
Filtering: select palindromes with length >= 7 using if len(word) >= 7.
Key concepts: accumulator (collects data during computation) and filtering (selects items by a condition).
Chapter 2: Create A Tuple
Tuple definition: an immutable sequence of values; created by a comma-separated list; parentheses are optional.
Single-element tuple: must include a trailing comma, e.g., (x,).
Empty tuple: use tuple() to create an empty tuple.
Type inspection: type(t) shows the type (e.g., int, str, tuple).
Naming caution: avoid using tuple as a variable name since it’s a built-in function.
Hashable property: tuples can be used as dictionary keys.
Creating examples: t = (1, 2, 3); t1 = (p,); tfromseq = tuple([1, 2, 3]).
Chapter 3: List To Tuple
Tuple and list operators: most list operators work with tuples as well (indexing t[0], slicing t[1:3]).
Concatenation and repetition: t1 + t2, t * n.
Sorting/reversing: sorted(t) returns a list; reversed(t) returns an iterator (convert if you need a tuple/list).
Immutability: tuples are immutable; lists are mutable.
Dictionary usage: tuples can be used as keys or values in dictionaries.
Chapter 4: A New Tuple
Tuple creation nuances: with or without parentheses; single-element requires a trailing comma.
Empty tuple and from sequence: () and tuple(sequence).
Operations: indexing, slicing, concatenation, and the star operator for duplication.
Sorting/reversing: sorted(t) yields a list; use tuple(reversed(t)) to obtain a tuple if needed.
Immutability demonstration: attempting to modify a tuple raises an error.
Dictionary usage: tuples as keys and as values; examples shown.
Chapter 5: Tuple Of Monty
Tuple packing/unpacking: a, b = 1, 2 assigns two values; username, domain = 'monty', 'python.org' via split (split returns a list).
Length matching: the left side must have the same number of variables as the right side values.
Unpacking in swaps: a, b = b, a to swap values.
For loops with tuples: for key, value in d.items(): … (direct unpacking of the tuple returned by items).
Chapter 6: Tuple Of Quotient
divmod: quotient, remainder = divmod(x, y); returns a tuple (q, r).
min and max: low, high = min_max(seq) returns a tuple (min, max).
Argument packing: def mean(*args): return sum(args) / len(args) collects any number of positional arguments into a tuple.
Unpacking arguments: mean(*t) unpacks a tuple t into separate arguments.
Demonstration: divmod(*t) where t is a two-element tuple (e.g., (7, 3)).
Chapter 7: Conclusion
Trimmed mean pattern: low, high = min_max(args); trimmed = list(args); remove(low) and remove(high); mean(trimmed).
Steps: compute min/max, convert to a mutable list to remove, then compute mean of remaining values.
Real-world use: leveraged in subjective judging (e.g., diving, gymnastics) to reduce outlier impact.
Example flow: mean(1, 2, 3) = 2; trimmed mean of (1, 2, 3, 10) is 2.5.
Key ideas: packing/unpacking, divmod, and flexible function argument handling with *args and argument unpacking.
If you need clarification on a specific example, ask and I can walk through it step by step.