December 2018
Beginner to intermediate
796 pages
19h 54m
English
Let's finish the tour of the number department with a look at fractions and decimals. Fractions hold a rational numerator and denominator in their lowest forms. Let's see a quick example:
>>> from fractions import Fraction>>> Fraction(10, 6) # mad hatter?Fraction(5, 3) # notice it's been simplified>>> Fraction(1, 3) + Fraction(2, 3) # 1/3 + 2/3 == 3/3 == 1/1Fraction(1, 1)>>> f = Fraction(10, 6)>>> f.numerator5>>> f.denominator3
Although they can be very useful at times, it's not that common to spot them in commercial software. Much easier instead, is to see decimal numbers being used in all those contexts where precision is everything; for example, in scientific and financial calculations.
Read now
Unlock full access