Python Tutorial: Multiply and Divide Fractions
This tutorial will guide you through multiplying and dividing fractions using Python. We’ll focus on clear, step-by-step explanations and practical code examples.
Understanding Fractions
A fraction represents a part of a whole. It consists of two parts: a numerator (the number of parts we have) and a denominator (the total number of parts).
- Numerator: The top number of a fraction.
- Denominator: The bottom number of a fraction.
For example, in the fraction 3/4, the numerator is 3, and the denominator is 4. This means we have 3 out of 4 parts.
Example 1: Multiply Fractions
Let’s start with multiplying two fractions. The rule is: (a/b) (c/d) = (ac) / (bd)
# Define the numerators and denominators of the fractions
numerator1 = 3
denominator1 = 4
numerator2 = 2
denominator2 = 5
# Multiply the numerators
product_numerator = numerator1 numerator2
# Multiply the denominators
product_denominator = denominator1 denominator2
# Calculate the result
result = product_numerator / product_denominator
# Print the result
print(f"The result of {numerator1}/{denominator1} {numerator2}/{denominator2} is: {result}")
Let’s break down the code:
- We initialize variables to hold the numerators and denominators of our two fractions.
- We multiply the numerators: 3 2 = 6.
- We multiply the denominators: 4 5 = 20.
- We divide the product of the numerators by the product of the denominators: 6 / 20 = 0.3.
- Finally, we print the result using an f-string for easy formatting.
Input: Numerator of first fraction (3), Denominator of first fraction (4), Numerator of second fraction (2), Denominator of second fraction (5)
Intermediate Calculations: product_numerator (6), product_denominator (20), result (0.3)
Output:
The result of 3/4 2/5 is: 0.3
Example 2: Handling Different Fractions
Now, let’s multiply a fraction by a decimal.
# Define the fractions
numerator = 1
denominator = 8
decimal = 0.5
# Convert the decimal to a fraction (optional but good practice)
# 0.5 = 1/2
fractional_decimal = 1/2
# Multiply the fraction by the decimal
result = numerator / denominator fractional_decimal
# Print the result
print(f"The result of {numerator}/{denominator} {decimal} is: {result}")
In this example, we first convert the decimal 0.5 to its fractional equivalent (1/2). Then we multiply the fraction 1/8 by 1/2, which gives us 1/16.
Input: Numerator (1), Denominator (8), Decimal (0.5)
Intermediate Calculations: fractional_decimal (0.5), result (0.0625)
Output:
The result of 1/8 0.5 is: 0.0625
Example 3: Dividing Fractions
Dividing fractions is the same as multiplying by the reciprocal of the second fraction. The reciprocal of a fraction (a/b) is (b/a).
# Define the fractions
numerator1 = 2
denominator1 = 3
numerator2 = 4
denominator2 = 5
# Calculate the reciprocal of the second fraction
reciprocal_denominator2 = denominator2
# Alternatively: denominator2 = 5 -> reciprocal_denominator2 = 1/5
# Multiply the first fraction by the reciprocal of the second fraction
result = numerator1 / denominator1 reciprocal_denominator2
# Print the result
print(f"The result of {numerator1}/{denominator1} / {numerator2}/{denominator2} is: {result}")
In this case, we’re dividing 2/3 by 4/5. To do this, we multiply 2/3 by the reciprocal of 4/5, which is 5/4.
Input: Numerator of first fraction (2), Denominator of first fraction (3), Numerator of second fraction (4), Denominator of second fraction (5)
Intermediate Calculations: reciprocal_denominator2 (5/5 or 1/5), result (0.75)
Output:
The result of 2/3 / 4/5 is: 0.75
Conclusion
You’ve now learned how to multiply and divide fractions in Python. Remember the key rules: multiply numerators, multiply denominators, and use the reciprocal when dividing. Practice with different fractions to solidify your understanding.



Leave a Reply