Why Sorting Fractions Matters in Math and Python
Sorting fractions in Python is a valuable skill for junior secondary students who are learning to connect mathematics with coding. Instead of simply converting fractions into decimals, this tutorial shows you how to compare and arrange fractions step by step using the LCM method. By the end, you'll know how to write a Python program to sort fractions in ascending and descending order, making it easier to solve math problems and strengthen your programming logic.
In this guide, we'll start with the basics of comparing fractions, then move on to a clear Python code example for sorting fractions. You'll see how to rationalise fractions, apply the least common multiple (LCM), and use Python's built-in functions to arrange them correctly. This approach not only improves your understanding of fractions but also builds confidence in writing simple algorithms.
Whether you're a student practicing for class, a teacher preparing classroom exercises, or a beginner looking for a Python fractions tutorial, this lesson is designed to be easy to follow. Let's dive in and learn how to sort fractions step by step with Python.
Rationalise (Canonise) the Fractions before Sorting | Maths Explanation for Python Kids
Just like was shown with Adding Fractions in Python and
Subtractinging Fractions in Python,
fractional numbers also have to be rationalised before sorting.
This means they are put in a form where their denominators become the same. This identical denominator is the LCM of the
previous denominators of all the separate fractions.
After this is done, the new numerators can easily be sorted in a preferred order.
Step-by-Step Guide to Sorting Fractions in Python
The following steps will guide us in writing our Python code for sorting fractions.
Let's illustrate the steps to follow with the example case 5/9,
3/7, 1/2
Step 1:
Using the Find LCM in Python
class from the Primary Category, find the LCM of the denominators.
⇒ LCM of 9, 7 & 2 = 126
Step 2:
In a turn by turn fashion, divide the found LCM from Step 1
by each denominator, multiplying the quotient by the corresponding numerator.
⇒
((5 x 14), (3 x 18), (1 x 63))/126
= (70, 54, 63)/126
Step 3:
Go ahead and sort the numerators in our order of choice.
⇒ In ascending order:
54/126, 63/126,
70/126
=
3/7, 1/2,
5/9
Create a new Python module file;
call it SortFraction.py.
Type out the adjoining Python code for sorting fractions in ascending and descending orders.
Note: You can comment out the SubtractFraction Python object
code in the main class from the previous lesson or simply continue from where it stopped.
So! Python Fun Practice Exercise - Sort 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 sorts those fractions.
Python Code for Sorting Fractions - Module File
# A class with a parent
class ArrangeFraction(PlusFraction):
# A constructor
def __init__(self, fractions):
super().__init__(fractions)
self.final_numerators = []
self.final_denominators = []
# Returns a dictionary of the new fraction
def sortAscending(self):
# STEPS 1, 2:
self.canoniseFraction()
self.copy_numerators = []
for member in self.new_numerators: self.copy_numerators.append(member);
# STEP 3:
# the little difference lies here(sort ascending)
self.copy_numerators.sort()
# map sorted (transformed) fractions to the original ones
for numerator in self.copy_numerators:
# get index using list value
self.position = self.new_numerators.index(numerator)
self.final_numerators.append(self.numerators[self.position])
self.final_denominators.append(self.denominators[self.position])
return {'numerators':self.final_numerators, 'denominators':self.final_denominators}
# Returns a dictionary of the new fraction
def sortDescending(self):
# STEPS 1, 2:
self.canoniseFraction()
self.copy_numerators = []
for member in self.new_numerators: self.copy_numerators.append(member);
# STEP 3:
# the little difference lies here(sort ascending)
self.copy_numerators.sort(reverse=True)
# map sorted (transformed) fractions to the original ones
for numerator in self.copy_numerators:
# get index using list value
self.position = self.new_numerators.index(numerator)
self.final_numerators.append(self.numerators[self.position])
self.final_denominators.append(self.denominators[self.position])
return {'numerators':self.final_numerators, 'denominators':self.final_denominators}
Python Code for Sorting Fractions - Main Class
from SortFraction import ArrangeFraction
##
# Sorting fractions
##
numerators = [1, 3, 5, 9]
denominators = [2, 4, 2, 10]
fractions = {'numerators':numerators, 'denominators':denominators}
print("\n Sorting in ascending order the fractions:\n")
# Print as fraction
for numerator in fractions['numerators']: print('{:12d}'.format(numerator), end='')
print('{}{:>11}'.format('\n', ' '), end='')
for wasted in range(len(numerators)-1): print('{}'.format('- , '), end='')
print('{:>1}'.format('-'))
for denominator in fractions['denominators']: print('{:12d}'.format(denominator), end='')
print('\n\n')
# use the SortFraction class
sort_fract = ArrangeFraction(fractions)
fractions = sort_fract.sortAscending()
# Print as fraction
for numerator in fractions['numerators']: print('{:13d}'.format(numerator), end='')
print('{}{:>1}'.format('\nAnswer = ', ' '), 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')
print('\n\n')