fractionsEasy Examples

Rational number arithmetic (exact fractions like 1/3)

Getting started with fractions

Basic import and usage of the fractions module.

python
from fractions import Fraction

f1 = Fraction(1, 3)
f2 = Fraction(1, 6)
print(f"{f1} + {f2} = {f1 + f2}")
print(f"0.75 as fraction: {Fraction(0.75)}")
print(f"'3/7' as fraction: {Fraction('3/7')}")

The fractions module is part of Python's standard library. Rational number arithmetic (exact fractions like 1/3).

Common fractions operations

Frequently used functions from the fractions module.

python
# More fractions examples
import fractions

print(f"fractions module loaded successfully")
print(f"Location: {fractions.__name__}")
print(f"Has {len(dir(fractions))} attributes")

These are the most commonly used features of the fractions module.

Want to try these examples interactively?

Open Easy Playground