AI Skill Report Card

Using Python Numeric Module

B+74·Sep 5, 2026·Source: Web
13 / 15
Python
from 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
12 / 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 — decimal for base-10 precision, fractions for exact ratios, statistics for descriptive stats, random for sampling/simulation.
  • Step 3: Set context/precision if using decimal (getcontext().prec).
  • Step 4: Perform operations using module-native types; avoid mixing raw float into Decimal/Fraction ops 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
14 / 20

Example 1: Financial calculation avoiding float error Input:

Python
0.1 + 0.2

Output:

0.30000000000000004  # float imprecision

Fix:

Python
from decimal import Decimal Decimal("0.1") + Decimal("0.2") # Decimal('0.3')

Example 2: Exact fraction reduction Input:

Python
from fractions import Fraction Fraction(6, 8)

Output:

Fraction(3, 4)  # auto-reduced to lowest terms

Example 3: Statistics with outliers Input:

Python
import 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
  • Use Decimal for money/financial data — never float.
  • Set decimal.getcontext().prec explicitly when precision matters beyond the default 28 digits.
  • Construct Decimal from 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.median over mean when data has outliers or skew.
  • Use statistics.fmean for faster float-based mean when precision of Decimal-level accuracy isn't required.
  • Check isinstance(x, numbers.Number) (from the numbers ABC hierarchy) when writing generic numeric code that should accept int, float, Decimal, complex, etc.
  • Don't mix float and Decimal in arithmetic — raises TypeError. Convert explicitly first.
  • Don't assume Fraction arithmetic 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 mean pstdev (population stdev) — they differ and silently give wrong results for full-population data.
  • Don't rely on default Decimal context precision across threads without setting localcontext() — context is thread-local and can cause inconsistent precision in concurrent code.
  • Don't use random module functions for cryptographic purposes — use secrets instead.
0
Grade B+AI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
13/15
Workflow
12/15
Examples
14/20
Completeness
16/20
Format
13/15
Conciseness
13/15