Share your repls and programming experiences

← Back to all posts
Defining the Rational Number Type (Q) in Python
kurner (6)

Although Python has a Fraction type (Rational Number), that hasn't always been true.

What would a Fraction type look like in pure Python? You can learn a lot of Python by leveraging your knowledge of how fractions work.

Going the other way: if you can already read Python, then here's a way to refamiliarize yourself with fractions.

The set of Rationals is known as Q, so the type name is Q too, but what you name it is up to you. You might prefer Rat.

Say < means "is a subset of", then this is true:

N < Z < Q < R < C

N = Natural Numbers; Z = Integers; Q = Rationals; R = Reals; C = Complex