← Python EnglishChapter 12 of 13

Standard Library Overview

## Learning Objectives - Explore the Python standard library - Learn about useful built-in modules - Master common library patterns ## Collections ### Counter ```python from collections import Counter words = ["apple", "banana", "apple", "cherry", "banana", "apple"] counter = Counter(words) print(counter) # Counter({'apple': 3, 'banana': 2, 'cherry': 1}) print(counter.most_common(2)) # [('apple', 3), ('banana', 2)] ``` ### defaultdict ```python from collections import defaultdict dd = defaultdict(list) # Default factory dd["fruits"].append("apple") dd["fruits"].append("banana") print(dd["fruits"]) # ['apple', 'banana'] print(dd["missing"]) # [] (empty list, not error) ``` ### deque ```python from collections import deque dq = deque([1, 2, 3]) dq.append(4) # Add to right dq.appendleft(0) # Add to left print(dq) # deque([0, 1, 2, 3, 4]) dq.pop() # Remove from right: 4 dq.popleft() # Remove from left: 0 ``` ### namedtuple ```python from collections import namedtuple Point = namedtuple("Point", ["x", "y"]) p = Point(10, 20) print(p.x, p.y) # 10 20 print(p[0], p[1]) # 10 20 ``` ## Itertools ### count, cycle, repeat ```python import itertools # Infinite iterator - count from 1 for i, num in zip(range(5), itertools.count(1)): print(num) # 1, 2, 3, 4, 5 # Cycle through iterable for i, item in zip(range(8), itertools.cycle(["a", "b", "c"])): print(item) # a, b, c, a, b, c, a, b # Repeat element for item in itertools.repeat("x", 3): print(item) # x, x, x ``` ### chain ```python import itertools list1 = [1, 2, 3] list2 = [4, 5, 6] combined = list(itertools.chain(list1, list2)) print(combined) # [1, 2, 3, 4, 5, 6] # chain.from_iterable nested = [[1, 2], [3, 4], [5, 6]] flat = list(itertools.chain.from_iterable(nested)) print(flat) # [1, 2, 3, 4, 5, 6] ``` ### compress, filterfalse ```python import itertools # Compress - select elements by filter data = ["a", "b", "c", "d"] selector = [True, False, True, False] filtered = list(itertools.compress(data, selector)) print(filtered) # ['a', 'c'] # filterfalse - opposite of filter numbers = [1, 2, 3, 4, 5, 6] evens = list(itertools.filterfalse(lambda x: x % 2, numbers)) print(evens) # [2, 4, 6] ``` ### islice, takewhile, dropwhile ```python import itertools # islice - slice iterator nums = range(10) sliced = list(itertools.islice(nums, 3, 8, 2)) print(sliced) # [3, 5, 7] # takewhile - take while condition is True nums = [1, 2, 3, 4, 5] taken = list(itertools.takewhile(lambda x: x < 3, nums)) print(taken) # [1, 2] # dropwhile - drop while condition is True, then take rest dropped = list(itertools.dropwhile(lambda x: x < 3, nums)) print(dropped) # [3, 4, 5] ``` ## functools ### lru_cache ```python import functools @functools.lru_cache(maxsize=None) def fibonacci(n): if n < 2: return n return fibonacci(n-1) + fibonacci(n-2) print(fibonacci(100)) # Fast with caching ``` ### partial ```python import functools def power(base, exponent): return base ** exponent square = functools.partial(power, exponent=2) cube = functools.partial(power, exponent=3) print(square(5)) # 25 print(cube(5)) # 125 ``` ### reduce ```python import functools numbers = [1, 2, 3, 4, 5] total = functools.reduce(lambda x, y: x + y, numbers) print(total) # 15 max_num = functools.reduce(lambda a, b: a if a > b else b, numbers) print(max_num) # 5 ``` ## re - Regular Expressions ### Basic Patterns ```python import re text = "My phone is 555-123-4567" # Search match = re.search(r'\d{3}-\d{3}-\d{4}', text) if match: print(match.group()) # 555-123-4567 # Find all numbers = re.findall(r'\d+', text) print(numbers) # ['555', '123', '4567'] # Replace new_text = re.sub(r'\d{3}-\d{3}-\d{4}', 'XXX-XXX-XXXX', text) print(new_text) # My phone is XXX-XXX-XXXX # Split parts = re.split(r'[\s,-]+', "a,b c-d") print(parts) # ['a', 'b', 'c', 'd'] ``` ### Pattern Flags ```python import re text = "Hello WORLD" print(re.findall(r'hello', text, re.IGNORECASE)) # ['Hello', 'WORLD'] print(re.findall(r'hello', text, re.I)) # Same multi = "line1\nline2\nline3" print(re.findall(r'^line', multi, re.MULTILINE)) # ['line1', 'line2', 'line3'] ``` ## urllib ```python from urllib.request import urlopen # GET request with urlopen("https://example.com") as response: content = response.read().decode("utf-8") print(content[:200]) ``` ## sqlite3 ```python import sqlite3 conn = sqlite3.connect("my.db") cursor = conn.cursor() # Create table cursor.execute(""" CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, name TEXT, email TEXT ) """) # Insert cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ("Alice", "alice@example.com")) # Query cursor.execute("SELECT * FROM users") for row in cursor.fetchall(): print(row) conn.commit() conn.close() ``` ## csv ```python import csv # Reading with open("data.csv", "r") as f: reader = csv.DictReader(f) for row in reader: print(row["name"], row["age"]) # Writing with open("output.csv", "w", newline="") as f: writer = csv.DictWriter(f, fieldnames=["name", "age"]) writer.writeheader() writer.writerow({"name": "Alice", "age": 30}) ``` ## logging ```python import logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" ) logger = logging.getLogger(__name__) logger.info("This is an info message") logger.warning("This is a warning") logger.error("This is an error") ``` ## json ```python import json data = { "name": "Alice", "age": 30, "skills": ["Python", "JavaScript"] } # Serialize json_str = json.dumps(data, indent=2, sort_keys=True) # Deserialize parsed = json.loads(json_str) # File operations with open("data.json", "w") as f: json.dump(data, f, indent=2) with open("data.json", "r") as f: loaded = json.load(f) ``` ## Summary - **collections**: Counter, defaultdict, deque, namedtuple - **itertools**: count, cycle, chain, islice, takewhile, dropwhile - **functools**: lru_cache, partial, reduce - **re**: Regular expressions for pattern matching - **urllib**: HTTP requests - **sqlite3**: SQLite database operations - **csv**: CSV file handling - **logging**: Application logging - **json**: JSON serialization/deserialization

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →