2.1. Advanced Data Structures (Sequences & Collections)
Lists
-
Definition: Ordered, mutable (changeable) sequence of elements.
-
Creation:
my_list = [1, 2, 3],list(iterable) -
Indexing/Slicing:
list[start:stop:step](stop is exclusive). Negative indices count from end. -
Key Methods:
-
append(x)– Add item to end. -
extend(iterable)– Append all items from iterable. -
insert(i, x)– Insert at indexi. -
remove(x)– Remove first occurrence ofx(ValueError if not found). -
pop([i])– Remove and return item at indexi(default last). -
clear()– Remove all items. -
index(x[, start[, end]])– Return first index ofx. -
count(x)– Count occurrences ofx. -
sort(key=None, reverse=False)– In-place sort. -
reverse()– In-place reverse.
-
-
List Comprehension:
[expression for item in iterable if condition]. Creates new list.squares = [x**2 for x in range(10) if x % 2 == 0] -
Nested Lists: Lists containing other lists (multi-dimensional arrays).
Tuples
-
Definition: Ordered, immutable sequence. Once created, cannot change elements.
-
Creation:
my_tuple = (1, 2, 3),tuple(iterable). Single-element tuple requires trailing comma:(5,). -
When to Use: For fixed data (coordinates, days), dictionary keys (if elements are immutable), function returns (multiple values).
-
Packing/Unpacking:
packed = 1, 2, 3 # Packing a, b, c = packed # Unpacking
Dictionaries
-
Definition: Unordered (Python 3.7+ preserves insertion order) collection of key:value pairs. Keys must be immutable (str, int, tuple). Values can be any type.
-
Creation:
{},dict(),dict(key1=val1, key2=val2),dict(zip(keys, values)). -
Access:
dict[key](raises KeyError if missing),dict.get(key, default=None)(safe). -
Key Methods:
-
keys(),values(),items()– Return view objects. -
update([other])– Merge dictionaries. -
pop(key[, default])– Remove key and return value. -
popitem()– Remove and return last inserted (LIFO) pair. -
setdefault(key[, default])– Get value if key exists, else set to default and return. -
clear()– Remove all items.
-
-
Dictionary Comprehension:
{key_expr: value_expr for item in iterable if condition}. -
Iteration:
for k in d:,for k, v in d.items():.
Sets
-
Definition: Unordered collection of unique, immutable elements. Mathematical set operations.
-
Creation:
set()(empty),{1, 2, 3},set(iterable). Warning:{}creates empty dict, not set. -
Key Methods:
-
add(x)– Add element. -
remove(x)– Removex(KeyError if missing). -
discard(x)– Removexif present (no error). -
pop()– Remove and return an arbitrary element (KeyError if empty). -
clear()– Remove all. -
Mathematical Operations:
-
union()or|– All elements from both. -
intersection()or&– Common elements. -
difference()or-– Elements in first but not second. -
symmetric_difference()or^– Elements in either, not both.
-
-
-
Set Comprehension:
{expr for item in iterable if condition}. -
Frozen Set (
frozenset): Immutable version of set. Can be used as dictionary key.
Type Conversion Between Structures
list(tuple) # Tuple -> List
tuple(list) # List -> Tuple
set(list) # List -> Set (removes duplicates, order lost)
list(dict) # Dict -> List of keys
dict(list_of_tuples) # List of (key, value) tuples -> Dict
[!TIP] Common Pitfall:
setremoves duplicates and loses order.list(dict)gives keys, uselist(dict.items())for key-value pairs.
2.2. Functions
Defining and Calling
-
Definition: Reusable block of code. Defined with
def.def func_name(param1, param2='default'): """Docstring (optional)""" # body return result # Optional. Returns `None` if no return. -
Parameters vs Arguments: Parameters are variables in definition. Arguments are values passed in call.
-
Return: Can return multiple values as a tuple:
return a, b, c→ caller gets(a, b, c).
Function Parameters & Arguments
-
Positional Arguments: Must match order.
-
Keyword Arguments:
func(arg2=val2, arg1=val1). Order doesn't matter. -
Default Parameters: Defined in function header. Used if argument not provided.
-
Variable-Length Arguments:
-
*args– Collects extra positional arguments into a tuple. -
**kwargs– Collects extra keyword arguments into a dict.
def func(a, b, *args, **kwargs): print(a, b) # 1, 2 print(args) # (3, 4, 5) print(kwargs) # {'x': 10, 'y': 20} func(1, 2, 3, 4, 5, x=10, y=20) -
Scope and Namespaces (LEGB Rule)
-
Scope: Region where a name (variable/function) is accessible.
-
Namespaces: Mapping of names to objects. Stored in
dict(locals(),globals()). -
LEGB Resolution Order:
-
Local – Inside current function.
-
Enclosing – In enclosing (outer) function (for nested functions).
-
Global – At module level.
-
Built-in – Python built-in names (
len,print).
-
-
globalKeyword: Modify a global variable inside a function.x = 10 def func(): global x x = 20 # Modifies global x -
nonlocalKeyword: Modify a variable from an enclosing (non-global) scope (used in nested functions).
Lambda Functions
-
Syntax:
lambda arguments: expression -
Characteristics: Single expression, no statements, returns the expression result. Often used with
map(),filter(),sorted().square = lambda x: x**2 sorted(list_of_tuples, key=lambda x: x[1]) # Sort by second element
[!TIP] Exam Focus: Difference between
*args(tuple) and**kwargs(dict). Know LEGB order. Lambda is for simple, one-line functions.
2.3. String Manipulation (Advanced Methods)
Common String Methods (Categorized)
-
Case Conversion:
upper(),lower(),capitalize()(first char upper, rest lower),title()(each word's first upper),swapcase(). -
Searching:
find(sub)/rfind(sub)– Return index or -1.index(sub)/rindex(sub)– Return index or raiseValueError.startswith(prefix),endswith(suffix). -
Validation (Boolean):
isalpha(),isdigit(),isalnum(),isspace(),islower(),isupper(). -
Modification:
-
replace(old, new[, count]) -
strip()/lstrip()/rstrip()– Remove whitespace (or specified chars). -
split(sep=None, maxsplit=-1)– Returns list of substrings. -
join(iterable)– Concatenate strings in iterable with string as separator.
-
-
Formatting:
-
str.format():"{} {}".format(a, b),"{name} is {age}".format(name="Alice", age=30). -
f-strings (Python 3.6+):
f"{name} is {age}". Modern, fastest, most readable.
-
String Slicing & Concatenation
-
Slicing:
s[start:stop:step].s[::-1]reverses string. -
Concatenation:
+operator orjoin()for multiple strings.join()is more efficient for many strings.
[!TIP] Common Pitfall:
split()without argument splits on any whitespace and removes empty strings.strip()only removes from ends.find()vsindex()– one returns -1, other raises error.
2.4. File Input/Output (I/O)
Opening Files
-
open()Function:open(file, mode='r', encoding=None) -
Common Modes:
-
'r'– Read (default). Fails if file doesn't exist. -
'w'– Write. Overwrites existing file. Creates new. -
'a'– Append. Adds to end of file. -
'x'– Exclusive creation. Fails if file exists. -
'b'– Binary mode (e.g.,'rb'). -
't'– Text mode (default). -
'+'– Open for reading and writing (e.g.,'r+').
-
-
Best Practice –
withStatement: Automatically closes file, even on error.with open('file.txt', 'r') as f: data = f.read() # File automatically closed here
Reading
-
f.read([size])– Read entire file orsizebytes/characters. -
f.readline()– Read one line (including newline). -
f.readlines()– Read all lines into a list. -
Iteration:
for line in f:– Memory efficient, line-by-line.
Writing
-
f.write(str)– Write string to file. -
f.writelines(iterable)– Write each string from iterable (no newlines added automatically). -
'w'vs'a':'w'truncates (empties) file on open.'a'seeks to end.
CSV & JSON (Introduction)
-
CSV (Comma-Separated Values): Use
csvmodule.import csv with open('data.csv', 'r') as f: reader = csv.reader(f) # Returns list of rows (each row is list) for row in reader: print(row) -
JSON (JavaScript Object Notation): Use
jsonmodule. Converts between Python objects (dict, list, str, int, float, bool, None) and JSON string.import json with open('data.json', 'r') as f: data = json.load(f) # Deserialize JSON file -> Python dict/list with open('out.json', 'w') as f: json.dump(data, f, indent=4) # Serialize Python obj -> JSON file
[!TIP] Critical: Always use
with open(...). Know difference betweenread()(whole file) and line iteration (memory efficient).jsonhandles basic Python types only.
2.5. Error and Exception Handling
Syntax Errors vs Exceptions
-
Syntax Error: Code violates Python grammar (e.g., missing colon). Detected at compile time.
-
Exception: Error detected during execution (e.g.,
ZeroDivisionError,FileNotFoundError).
try, except, else, finally Blocks
try:
# Code that may raise exception
result = 10 / 0
except ZeroDivisionError as e:
# Handle specific exception
print(f"Cannot divide by zero: {e}")
except (TypeError, ValueError) as e:
# Handle multiple specific exceptions
print(f"Type or value error: {e}")
except Exception as e:
# Catch-all for any other exception (use cautiously)
print(f"Unexpected error: {e}")
else:
# Executes if NO exception raised in try block
print("Division successful:", result)
finally:
# Always executes (cleanup: close files, release resources)
print("Cleanup complete")
Raising Exceptions
-
raise Exception("message")– Raise built-in or custom exception. -
raise– Re-raise current exception in except block.if x < 0: raise ValueError("x must be non-negative")
Common Built-in Exceptions (Categorization)
-
Lookup Errors:
IndexError(sequence index out of range),KeyError(dict key not found). -
Type Errors:
TypeError(operation on inappropriate type, e.g.,str + int). -
Value Errors:
ValueError(right type but inappropriate value, e.g.,int('abc')). -
I/O Errors:
FileNotFoundError,PermissionError,IOError(base class). -
Runtime Errors:
ZeroDivisionError,OverflowError,RecursionError. -
Other:
AttributeError(attribute/method not found),ImportError/ModuleNotFoundError,KeyboardInterrupt(Ctrl+C).
[!TIP] Exam Strategy: Order of
exceptblocks matters – catch specific exceptions before general ones (likeException).finallyis for cleanup (e.g., closing file handles).elseruns only on success.