How To Multiply Fractions In Python

Fractions are important mathematical concepts used in many real-world problems. Python is a versatile programming language that can handle mathematical operations, including multiplying fractions. In this tutorial, we will explore how to multiply fractions in Python.

Step 1: Define the fractions

The first step in multiplying fractions in Python is to define the fractions. We can do this by assigning values to variables.

Here, we have defined two fractions: fraction1 and fraction2.

Step 2: Multiply the fractions

To multiply fractions in Python, we simply need to multiply the numerators and the denominators separately and then simplify the answer. We can achieve this by using the following formula:

To implement this in Python, we use the following code:

Here, we have multiplied the two fractions, stored the result in a variable called product, and printed the answer to the console.

Step 3: Simplify the answer

The answer we get from multiplying fractions may not always be in its simplest form. We can simplify the answer by finding the greatest common divisor (GCD) of the numerator and denominator and then dividing both by it.

To do this in Python, we can use the math module, which provides a gcd() function that returns the GCD of two numbers. We can use the gcd() function to simplify the answer as follows:

Here, we have imported the math module and used its gcd() function to find the GCD of the numerator and denominator of the product we obtained in Step 2. We then divided the product by the GCD to get the simplified answer and printed it to the console.


In this tutorial, we have learned how to multiply fractions in Python. We defined the fractions, multiplied them, and simplified the answer. Python provides an easy and efficient way to handle mathematical operations involving fractions, making it a powerful tool for data analysis and scientific computing.

Full code: