A comprehensive, production-grade Python reference suite demonstrating exact rational arithmetic, conversions, float approximations, rounding, range sequences, and memory introspection using Python's standard library (fractions.Fraction, math, decimal, range).
This directory has been refactored and flattened from nested hyphenated subdirectories (01-Fundamentals, 02-Advanced-Math-and-Operators, 03-Range-Evolution-and-Performance) into a clean, PEP 8-compliant 5-tier architecture:
fraction_basics_ops.py: Instantiation from integers, strings, floats, and Decimals (Fraction(numerator, denominator)), automated GCD reduction, and attribute access (.numerator,.denominator).fraction_conversions_ops.py: Rational float approximations (limit_denominator()), integer ratio extraction (as_integer_ratio()), primitive type conversions (float,int,str), and numerator GCD calculations (math.gcd()).fraction_arithmetic_ops.py: Exact arithmetic (+,-,*,/,//,%,**), divmod evaluation (divmod(a, b)), relational comparisons (==,!=,>,<,>=,<=), and mixed-type arithmetic with integers and floats.fraction_advanced_math_ops.py: Mathematical rounding (math.floor,math.ceil,math.trunc,round()), precision summation over iterables (sum()), and bi-directional Decimal interop.fraction_range_evolution_ops.py: Fractional sequence range generation,range()O(1) space memory footprint analysis,dir(range)introspection, and Python version evolution timeline (Python 2.7 to 3.13).test_fractions.py: Master unittest test suite running 21 test cases validating all 5 fraction modules.
fractions.Fraction: Standard library class for exact rational number arithmetic and representation.math: Mathematical functions (floor,ceil,trunc,gcd).decimal.Decimal: High-precision fixed-point decimal arithmetic interop.sys: System-specific memory size inspection (sys.getsizeof).
Creates a rational fraction from integer numerator and denominator. Automatically divides both by their Greatest Common Divisor (GCD) to simplify to lowest terms.
from fractions import Fraction
f = Fraction(4, 14)
print(f) # Output: '2/7'Creates a Fraction instance from a parsed string, binary float, or Decimal object.
from decimal import Decimal
f_str = Fraction(" 3 / 8 ") # Output: 3/8
f_float = Fraction(0.25) # Output: 1/4
f_dec = Fraction(Decimal("0.125")) # Output: 1/8Returns the integer numerator of the fraction in lowest terms.
Returns the integer denominator of the fraction in lowest terms (always positive).
f = Fraction(-6, 8) # Simplified to Fraction(-3, 4)
print(f.numerator) # -3
print(f.denominator) # 4Finds and returns the closest Fraction to self that has a denominator at most max_denominator. Useful for approximating binary floats.
val = 0.3333333333333333
f = Fraction(val).limit_denominator(10)
print(f) # Output: 1/3Returns a 2-tuple pair (numerator, denominator) of integers. Standardized across int, float, and Fraction in Python 3.8+.
f = Fraction(3, 4)
print(f.as_integer_ratio()) # Output: (3, 4)Supports exact rational arithmetic returning a reduced Fraction:
a + b(Addition)a - b(Subtraction)a * b(Multiplication)a / b(Division)a // b(Floor Division, returnsint)a % b(Modulo, returnsFraction)a ** n(Exponentiation)
Returns a tuple (quotient, remainder) where quotient = a // b (int) and remainder = a % b (Fraction).
a = Fraction(7, 3)
b = Fraction(2, 3)
print(divmod(a, b)) # Output: (3, Fraction(1, 3))math.floor(f): Returns largest integer<= f.math.ceil(f): Returns smallest integer>= f.math.trunc(f): Returns integer truncated towards zero.round(f): Rounds fraction to nearest integer.
import math
from fractions import Fraction
f = Fraction(7, 3)
print(math.floor(f)) # 2
print(math.ceil(f)) # 3
print(math.trunc(f)) # 2.start: Starting integer bound..stop: Ending integer bound (exclusive)..step: Step increment value..index(value): Returns zero-based index of value..count(value): Returns 1 if value exists in range, 0 otherwise.
r = range(10, 100, 5)
print(r.start, r.stop, r.step) # 10 100 5
print(r.index(25)) # 3| Module | Primary Operations | Description |
|---|---|---|
fraction_basics_ops.py |
Fraction(), .numerator, .denominator |
Instantiation from int, string, float, Decimal, and component extraction. |
fraction_conversions_ops.py |
limit_denominator(), as_integer_ratio(), math.gcd |
Float approximations, tuple extraction, numeric casting, and GCD. |
fraction_arithmetic_ops.py |
+, -, *, /, //, %, divmod, == |
Exact arithmetic, quotient/remainder, relational comparisons, mixed types. |
fraction_advanced_math_ops.py |
floor, ceil, trunc, round, sum(), Decimal |
Mathematical rounding, exact list summation, and Decimal interop. |
fraction_range_evolution_ops.py |
generate_fractional_range, dir(range) |
Fractional step sequences, memory benchmarking, and Python version history. |
test_fractions.py |
unittest.TestCase |
Master test suite validating all 21 fraction operations. |
python3 fraction_basics_ops.py
python3 fraction_conversions_ops.py
python3 fraction_arithmetic_ops.py
python3 fraction_advanced_math_ops.py
python3 fraction_range_evolution_ops.pypython3 -m unittest test_fractions.pypython3 -m py_compile *.py