This project is a simple math calculator.  You input either first number, operand, second number or first number, operand.  It does basic math: *, /, +, and -.  It does powers, factorials, circumference and area of a circle.

my_functions.py code

I placed all the arguments that will be called into one file called my_functions.py.  I will be calling them into the other programs I am running.

get_truth() uses relative operators so that you can use either words or symbols for the math functions. it lists each keyword and value
math_func() uses if – elif statements. I also use
{:.xf}”.format(result) to round the numbers where x is the precision
get_truthWQ() allows the operations in words without quotes.  It is 2 lines.

It also uses Fraction to display the number as a fraction instead of a decimal.

#!/usr/bin/python3
import math
import operator


def get_truth(firstnum, op, secondnum):
    rel_ops = {
        '+': operator.add,
        '-': operator.sub,
        '*': operator.mul,
        '/': operator.truediv,
        '//': operator.floordiv,
        'operator.add': operator.add,
        'operator.sub': operator.sub,
        'operator.mul': operator.mul,
        'operator.truediv': operator.truediv,
        'operator.floordiv': operator.floordiv,
        '>': operator.gt,
        '<': operator.lt,
        '>=': operator.ge,
        '<=': operator.le,
        '==': operator.eq,
        '!=': operator.ne,
        'operator.gt': operator.gt,
        'operator.lt': operator.lt,
        'operator.ge': operator.ge,
        'operator.le': operator.le,
        'operator.eq': operator.eq,
        'operator.ne': operator.ne
    }
    return rel_ops[op](firstnum, secondnum);


def get_truthWQ(firstnumWQ=0, op=0, secondnumWQ=0):
    return op(firstnumWQ, secondnumWQ);


def math_func(firstnum=0, operand=0, secondnum=0):
    if operand == '*':
        print(firstnum, '*', secondnum, '=', firstnum * secondnum)
        return;
    elif operand == '/':
        result = firstnum / secondnum
        if result - int(result) == 0:
            print(firstnum, '/', secondnum, '=', int(result))
        else:
            print(firstnum, '/', secondnum, '=', "{:.4f}".format(result))
        return;
    elif operand == '+':
        result = firstnum + secondnum
        print(firstnum, '+', secondnum, '=', result)
        return;
    elif operand == '-':
        result = firstnum - secondnum
        print(firstnum, '-', secondnum, '=', result)
        return;
    elif operand == 'sqroot':
        result = math.sqrt(firstnum)
        if result - int(result) == 0:
            print('square root of', firstnum, '=', int(result))
        else:
            print('square root of', firstnum, '=', "{:.4f}".format(result))
        return;
    elif operand == 'powerof':
        from fractions import Fraction
        result = math.pow(firstnum, secondnum)
        # if the result returns an integer, it will only print the integer, else it will print 2 decimal places
        # This also prints the fractional value not the decimal
        if result - int(result) == 0:
            print(firstnum, 'raised to the power of', Fraction(secondnum).limit_denominator(100), '=', int(result)
                  )
        else:
            print(firstnum, 'raised to the power of', Fraction(secondnum).limit_denominator(100),
                  '=', "{:.2f}".format(result))
        return;
    elif operand == 'fact':
        result = math.factorial(firstnum)
        print(firstnum, '! is =', result)
        return;
    elif operand == 'circarea':
        result = math.pi * firstnum ** 2
        print('area of circle with radius of', firstnum, 'is', "{:.4f}".format(result))
        return;
    elif operand == 'circcircum':
        result = math.pi * 2 + firstnum
        print('circumference of circle with radius of', firstnum, 'is', "{:.4f}".format(result))
        return;
    elif firstnum == operand == secondnum == 0:
        print('you forgot the all the inputs')
        return;
    elif operand == secondnum == 0:
        print('you forgot the operand')
        return;

Arguments.py

You have basic math, sqroot, fact for factorial, powerof, cirarea, circircum for circle area and circumference
#!/usr/bin/env python3


from my_functions import *


math_func()
math_func(5)
math_func(5, '*', 4)
math_func(6, '+', 4)
math_func(10, '/', 2)
math_func(7, '/', 3)
math_func(8, '-', 4)
math_func(400, 'sqroot')
# even adding the secondnum will not break it because it isn't used
math_func(5, 'sqroot', 1)
math_func(200, 'powerof', 2)
math_func(200, 'powerof', 3)
# raising a number to .5 is same as square root
math_func(16, 'powerof', 0.5)
# cubed root of a number
math_func(27, 'powerof', (1 / 3))
# fourth root of a number
math_func(81, 'powerof', (1 / 4))
math_func(339019928, 'powerof', .05)
math_func(10, 'fact')
math_func(4, 'circarea')
math_func(4, 'circcircum')

The output

you forgot the all the inputs
you forgot the operand
5 * 4 = 20
6 + 4 = 10
10 / 2 = 5
7 / 3 = 2.3333
8 - 4 = 4
square root of 400 = 20
square root of 5 = 2.2361
200 raised to the power of 2 = 40000
200 raised to the power of 3 = 8000000
16 raised to the power of 1/2 = 4
27 raised to the power of 1/3 = 3
81 raised to the power of 1/4 = 3
339019928 raised to the power of 1/20 = 2.67
10 ! is = 3628800
area of circle with radius of 4 is 50.2655
circumference of circle with radius of 4 is 10.2832

 

Process finished with exit code 0

I am passing the relative operator in quotes with the variable to the operator function.

import math
import operator



def get_truth(firstnum, op, secondnum):
rel_ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'//': operator.floordiv,
'operator.add': operator.add,
'operator.sub': operator.sub,
'operator.mul': operator.mul,
'operator.truediv': operator.truediv,
'operator.floordiv': operator.floordiv,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'==': operator.eq,
'!=': operator.ne,
'operator.gt': operator.gt,
'operator.lt': operator.lt,
'operator.ge': operator.ge,
'operator.le': operator.le,
'operator.eq': operator.eq,
'operator.ne': operator.ne
}
return rel_ops[op](firstnum, secondnum);

print('Testing MDAS')
print('17 + 9 =', get_truth(17, '+', 9))
print('17 - 9 =', get_truth(17, '-', 9))
print('17 * 9 =', get_truth(17, '*', 9))
print('17 / 9 =', get_truth(17, '/', 9))
print('17 // 9 =', get_truth(17, '//', 9))
print('')
print('17 operator.add 9 =', get_truth(17, 'operator.add', 9))
print('17 operator.sub 9 =', get_truth(17, 'operator.sub', 9))
print('17 operator.mul 9 =', get_truth(17, 'operator.mul', 9))
print('17 operator.truediv 9 =', get_truth(17, 'operator.truediv', 9))
print('17 operator.floordiv 9 =', get_truth(17, 'operator.floordiv', 9))
print('')
print('Testing truth or false')
print('1.0 > 0.0 is', get_truth(1.0, '>', 0.0))
print('1.0 < 0.0 is', get_truth(1.0, '<', 0.0))
print('1.0 >= 0.0 is', get_truth(1.0, '>=', 0.0))
print('1.0 <= 0.0 is', get_truth(1.0, '<=', 0.0))
print('1.0 == 0.0 is',get_truth(1.0, '==', 0.0))
print('1.0 != 0.0 is',get_truth(1.0, '!=', 0.0))
print('')
print('1.0 operator.gt 0.0 is', get_truth(1.0, 'operator.gt', 0.0))
print('1.0 operator.lt 0.0 is', get_truth(1.0, 'operator.lt', 0.0))
print('1.0 operator.ge 0.0 is', get_truth(1.0, 'operator.ge', 0.0))
print('1.0 operator.le 0.0 is', get_truth(1.0, 'operator.le', 0.0))
print('1.0 operator.eq 0.0 is', get_truth(1.0, 'operator.eq', 0.0))
print('1.0 operator.ne 0.0 is', get_truth(1.0, 'operator.ne', 0.0))


print('')
print('Operator without quotes around it')
def get_truth(inp, op, cut):
return op(inp, cut);

print('1.0 operator.gt 0.0 is', get_truth(1.0, operator.gt, 0.0))
print('1.0 operator.lt 0.0 is', get_truth(1.0, operator.lt, 0.0))
print('1.0 operator.ge 0.0 is', get_truth(1.0, operator.ge, 0.0))
print('1.0 operator.le 0.0 is', get_truth(1.0, operator.le, 0.0))
print('1.0 operator.eq 0.0 is', get_truth(1.0, operator.eq, 0.0))
print('1.0 operator.ne 0.0 is', get_truth(1.0, operator.ne, 0.0))

The output

Testing MDAS
17 + 9 = 26
17 - 9 = 8
17 * 9 = 153
17 / 9 = 1.8888888888888888
17 // 9 = 1

17 operator.add 9 = 26
17 operator.sub 9 = 8
17 operator.mul 9 = 153
17 operator.truediv 9 = 1.8888888888888888
17 operator.floordiv 9 = 1

Testing truth or false
1.0 > 0.0 is True
1.0 < 0.0 is False
1.0 >= 0.0 is True
1.0 <= 0.0 is False
1.0 == 0.0 is False
1.0 != 0.0 is True

1.0 operator.gt 0.0 is True
1.0 operator.lt 0.0 is False
1.0 operator.ge 0.0 is True
1.0 operator.le 0.0 is False
1.0 operator.eq 0.0 is False
1.0 operator.ne 0.0 is True

Operator without quotes around it
1.0 operator.gt 0.0 is True
1.0 operator.lt 0.0 is False
1.0 operator.ge 0.0 is True
1.0 operator.le 0.0 is False
1.0 operator.eq 0.0 is False
1.0 operator.ne 0.0 is True