# Fractions in Python

Published on (Updated: 02 May 2020) Welcome to the Fraction Math in Python page! Here, you'll find the source code for this program as well as a description of how the program works.

## Current Solution

``````import operator
import sys
from fractions import Fraction

d = {
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv,
"==": lambda x, y: int(operator.eq(x, y)),
"<": lambda x, y: int(operator.lt(x, y)),
">": lambda x, y: int(operator.gt(x, y)),
"<=": lambda x, y: int(operator.le(x, y)),
">=": lambda x, y: int(operator.ge(x, y)),
"!=": lambda x, y: int(operator.ne(x, y)),
}

def main(args):
if len(args) != 3:
print("Usage: ./fraction-math operand1 operator operand2")
sys.exit(1)
else:
try:
o1 = Fraction(args)
except ValueError:
print(f"Invalid operand: {args}")
try:
o2 = Fraction(args)
except ValueError:
print(f"Invalid operand: {args}")
try:
print(d[args](o1, o2))
except KeyError:
print(f"Invalid operator: {args}")

if __name__ == "__main__":
main(sys.argv[1:])
``````

Fraction Math in Python was written by:

• Jeremy Grifski
• Niraj Kamdar

If you see anything you'd like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of May 10 2022 16:42:22. The solution was first committed on Oct 16 2019 12:52:44. As a result, documentation below may be outdated.

## How to Implement the Solution

Let's first take a look at the solution.

``````import operator
import sys
from fractions import Fraction

d = {
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv,
"==": operator.eq,
"<": operator.lt,
">": operator.gt,
"<=": operator.le,
">=": operator.ge,
"!=": operator.ne,
}

def main(args):
if len(args) != 3:
print("Usage: python fraction.py operand1 operator operand2")
sys.exit(1)
else:
try:
o1 = Fraction(args)
except ValueError:
print("Invalid operand: {}".format(args))
try:
o2 = Fraction(args)
except ValueError:
print("Invalid operand: {}".format(args))
try:
print(d[args](o1, o2))
except KeyError:
print("Invalid operator: {}".format(args))

if __name__ == "__main__":
main(sys.argv[1:])

``````

### Imports

In our sample, we import three standard library utilities:

``````import operator
import sys
from fractions import Fraction
``````

Here, we have imported sys for taking arguments from console. operator are to perform artithmatic and relational operation. Fraction class provide various methods for working with fractions.

### Mapping operator

``````d = {
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv,
"==": operator.eq,
"<": operator.lt,
">": operator.gt,
"<=": operator.le,
">=": operator.ge,
"!=": operator.ne,
}
``````

Here, we are mapping operator entered in the string to actual operator method so that Fraction class can perform that.

### Check number of arguments

Our main function takes arguments as parameter.

``````def main(args):
if len(args) != 3:
print("Usage: python fraction.py operand1 operator operand2")
sys.exit(1)
``````

Here, we check if no. of arguments entered are three, if it's not then print `Usage: python fraction.py operand1 operator operand2` on console and exit.

### Perform operation

``````    else:
try:
o1 = Fraction(args)
except ValueError:
print("Invalid operand: {}".format(args))
try:
o2 = Fraction(args)
except ValueError:
print("Invalid operand: {}".format(args))
try:
print(d[args](o1, o2))
except KeyError:
print("Invalid operator: {}".format(args))
``````

Now, we check if we can convert entered args into Fraction type if we can't then we print `Invalid operand: (entered operand)` on console. After that we check if operator is valid and if it's not then we print `Invalid operator: (entered operator)` on console. If everything is good then it prints desired output on console.

### Taking arguments from console

``````if __name__ == "__main__":
main(sys.argv[1:])
``````

Here, `sys.argv` contains arguments passed from the console. We know that first argument is name of file itself. so, all we need is arguments that are passed after that. We then give it to the main function.

## How to Run the Solution

To run the fractions operation in python program, grab a copy of the fractions.py file from GitHub. After that, get the latest version of python interpreter. Now, all you have to do is run the following from the command line:

``````python fraction.py "1/4" "+" "5/8"
``````

Alternatively, you can always copy the source code into an online python interpreter. Just make sure you pass some input to your program before you run it.