AI Skill Report Card
Using Python Numeric Module
Quick Start13 / 15
Pythonfrom decimal import Decimal from fractions import Fraction import statistics as stats # Precise decimal arithmetic (avoids float rounding issues) price = Decimal("19.99") * 3 print(price) # 59.97 # Exact rational arithmetic half = Fraction(1, 2) third = Fraction(1, 3) print(half + third) # 5/6 # Statistical summary data = [2, 4, 4, 4, 5, 5, 7, 9] print(stats.mean(data), stats.median(data), stats.stdev(data))
Recommendation▾
Add an example demonstrating the `numbers` ABC hierarchy usage (isinstance checks) since it's mentioned in description and pitfalls but never shown concretely
Workflow12 / 15
Progress checklist for choosing/using the right numeric tool:
- Step 1: Identify the problem type (exact decimal money math, exact rational fractions, general stats, or random sampling).
- Step 2: Pick the module —
decimalfor base-10 precision,fractionsfor exact ratios,statisticsfor descriptive stats,randomfor sampling/simulation. - Step 3: Set context/precision if using
decimal(getcontext().prec). - Step 4: Perform operations using module-native types; avoid mixing raw
floatintoDecimal/Fractionops without explicit conversion. - Step 5: Validate results (check rounding mode, denominator reduction, or statistical assumptions like normality).
- Step 6: Convert back to desired output type (
float,str,int) only at the boundary (display/storage).
Recommendation▾
Include a bad-outcome example (e.g., mixing float and Decimal causing TypeError) rather than only listing it in pitfalls prose
Examples14 / 20
Example 1: Financial calculation avoiding float error Input:
Python0.1 + 0.2
Output:
0.30000000000000004 # float imprecision
Fix:
Pythonfrom decimal import Decimal Decimal("0.1") + Decimal("0.2") # Decimal('0.3')
Example 2: Exact fraction reduction Input:
Pythonfrom fractions import Fraction Fraction(6, 8)
Output:
Fraction(3, 4) # auto-reduced to lowest terms
Example 3: Statistics with outliers Input:
Pythonimport statistics as stats data = [10, 12, 11, 13, 100] stats.mean(data), stats.median(data)
Output:
(29.2, 12) # median is robust to the outlier; mean is skewed
Recommendation▾
Workflow checklist is more of a decision guide than a sequential process — could tighten by merging with Quick Start or restructuring around concrete task scenarios
Best Practices
- Use
Decimalfor money/financial data — neverfloat. - Set
decimal.getcontext().precexplicitly when precision matters beyond the default 28 digits. - Construct
Decimalfrom strings (Decimal("0.1")), not floats (Decimal(0.1)), to avoid inheriting float imprecision. - Use
Fraction.from_float()or.limit_denominator()when converting floats to fractions to prevent unwieldy denominators. - Prefer
statistics.medianovermeanwhen data has outliers or skew. - Use
statistics.fmeanfor faster float-based mean when precision ofDecimal-level accuracy isn't required. - Check
isinstance(x, numbers.Number)(from thenumbersABC hierarchy) when writing generic numeric code that should accept int, float, Decimal, complex, etc.
Common Pitfalls
- Don't mix
floatandDecimalin arithmetic — raisesTypeError. Convert explicitly first. - Don't assume
Fractionarithmetic is fast for large-denominator chains — it can grow numerators/denominators unboundedly; consider periodic.limit_denominator(). - Don't use
statistics.stdev(sample stdev) when you meanpstdev(population stdev) — they differ and silently give wrong results for full-population data. - Don't rely on default
Decimalcontext precision across threads without settinglocalcontext()— context is thread-local and can cause inconsistent precision in concurrent code. - Don't use
randommodule functions for cryptographic purposes — usesecretsinstead.