On this page
numbers.Rational
Prev: numbers.Number | Up: numbers | Next: numbers.Real
Rel: numbers.Complex, numbers.Integral, numbers.Number, numbers.Real
Ref: docs.python.org — numbers.Rational
Tags: #public #python3 #stdlib #numbers
Import
import numbers
from numbers import Rational
Signature & Overview
numbers.Rational()
To Real, Rational adds numerator and denominator properties.
Methods & Attributes
Rational.conjugate(self)— Conjugate is a no-op for Reals.Rational.denominator(...)— The denominator of a rational number in lowest terms.Rational.imag(...)— Real numbers have no imaginary component.Rational.numerator(...)— The numerator of a rational number in lowest terms.Rational.real(...)— Real numbers are their real component.
Usage
from fractions import Fraction
import numbers
assert isinstance(Fraction(1, 3), numbers.Rational)
assert isinstance(5, numbers.Rational)
assert not isinstance(0.5, numbers.Rational)
Gotchas
- Exposes
.numeratorand.denominator:floatis intentionally not aRational.
REPL Inspect
from numbers import Rational
help(Rational)
Linked from
Related notes
- tuts/python3/stdlib_module/numbers/Complex Linked here; links to this page; shared tags: #numbers, #stdlib
- tuts/python3/stdlib_module/numbers/Integral Linked here; links to this page; shared tags: #numbers, #stdlib
- tuts/python3/stdlib_module/numbers/Number Linked here; links to this page; shared tags: #numbers, #stdlib
- tuts/python3/stdlib_module/numbers/Real Linked here; links to this page; shared tags: #numbers, #stdlib
- tuts/python3/stdlib_module/numbers Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib