Basic Calculator
def calculate(a, op, b):
if op == '+': return a + b
if op == '-': return a - b
if op == '*': return a * b
if op == '/': return a / b
Simple arithmetic operations
Advanced Calculator
def advanced_calc(a, op, b):
operations = {
'+': lambda x, y: x + y,
'-': lambda x, y: x - y,
'*': lambda x, y: x * y,
'/': lambda x, y: x / y if y != 0 else 'Error'
}
return operations.get(op, 'Invalid op')(a, b)
Uses dictionary for cleaner code
Scientific Calculator
def scientific_calc(x, operation):
import math
ops = {
'sqrt': math.sqrt,
'square': lambda n: n ** 2,
'log': math.log10
}
return ops.get(operation, 'Invalid')(x)
Mathematical operations