Understanding the Math Behind Fraction Division | Maths Explanation for Python Kids
Python makes it easy to perform arithmetic operations like dividing fractions. Whether you're simplifying fractions, multiplying them, or coding a math class project, Python provides clear syntax and powerful tools for beginners and students alike.
Dividing fractions in Python is a great way to combine coding with math skills. In this tutorial, junior secondary students will learn how to use Python to divide fractions step-by-step. We'll explore how to invert and multiply fractions, write a Python class for fraction operations, and understand the logic behind the algorithm.
Division is the inverse operation of multiplication; That is exactly what we'll do with for fractions.
Invert the fractions that come after a division sign, as well as change the division sign to multiplication, and then
follow through with multiplication, as already explained in the
Multiplying Fractions with Python tutorial.
Algorithm Steps to Divide Fractions in Python
Say we are to implement a Python algorithm to divide the given fractional expression
21/8 ÷ 7/2;
Inverting this will yield
21/8 ÷ 7/2;
Then we can go ahead and use the Multiplying Fractions with Python class code to multiply the fractions.
Create a new Python module file;
Call it DivideFraction.pm.
Type out the adjoining Python code for dividing fractions.
Note: You can comment out the MultiplyFraction Python object code in the main class from the previous lesson or simply continue from where it stopped.
So! Python Fun Practice Exercise - Divide Fractions
As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators, and see how the Python code divides those fractions.
Python Code for Dividing Fractions - Module File
# A class with a parent
class DivFraction(TimesFraction):
# A constructor
def __init__(self, fractions):
super().__init__(fractions)
# Returns a dictionary of the new fraction
def doDivide(self):
# Invert every other fraction but the first
for count in range(1, len(self.numerators)):
self.temp = self.numerators[count]
self.numerators[count] = self.denominators[count]
self.denominators[count] = self.temp
return self.doMultiply()
Python Code for Dividing Fractions - Main Class
from DivideFraction import DivFraction
##
# Dividing fractions
##
numerators = [16, 9, 640, 7]
denominators = [9, 20, 27, 20]
fractions = {'numerators':numerators, 'denominators':denominators}
print('\n Solving:\n')
# Print as fraction
for numerator in fractions['numerators']: print('{:13d}'.format(numerator), end='')
print('{}{:>12}'.format('\n', ' '), end='')
for wasted in range(len(numerators)-1): print('{}'.format('- / '), end='')
print('{:>1}'.format('-'))
for denominator in fractions['denominators']: print('{:13d}'.format(denominator), end='')
print('\n\n')
# use the DivideFraction class
div_fract = DivFraction(fractions)
fraction = div_fract.doDivide()
print('{:25d}'.format(fraction['numerator']))
print('{:>25}'.format('Answer = -'))
print('{:25d}'.format(fraction['denominator']))
print('\n\n')