﻿ Python Math: Add, subtract, multiply and divide two fractions - w3resource

# Python Math: Add, subtract, multiply and divide two fractions

## Python Math: Exercise-46 with Solution

Write a Python program to add, subtract, multiply and divide two fractions.

Sample Solution:-

Python Code:

``````import fractions

f1 = fractions.Fraction(2, 3)
f2 = fractions.Fraction(3, 7)

print('{} + {} = {}'.format(f1, f2, f1 + f2))
print('{} - {} = {}'.format(f1, f2, f1 - f2))
print('{} * {} = {}'.format(f1, f2, f1 * f2))
print('{} / {} = {}'.format(f1, f2, f1 / f2))
```
```

Sample Output:

```2/3 + 3/7 = 23/21
2/3 - 3/7 = 5/21
2/3 * 3/7 = 2/7
2/3 / 3/7 = 14/9
```

Flowchart:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

Share this Tutorial / Exercise on : Facebook and Twitter

## Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

```str="+++Welcome python+++"
str.strip()

print(str)```

Output:

`+++Welcome python+++`