Python Program to Create a Simple Calculator
if statements to perform +, -, *, or / operations, like if op == '+' : result = num1 + num2.Examples
How to Think About It
if or elif. Perform the operation on the numbers and show the result. Handle special cases like division by zero to avoid errors.Algorithm
Code
num1 = float(input('Enter first number: ')) num2 = float(input('Enter second number: ')) operator = input('Enter operator (+, -, *, /): ') if operator == '+': result = num1 + num2 elif operator == '-': result = num1 - num2 elif operator == '*': result = num1 * num2 elif operator == '/': if num2 != 0: result = num1 / num2 else: print('Error: Division by zero is not allowed.') exit() else: print('Invalid operator') exit() print('Result:', result)
Dry Run
Let's trace the example where num1=5, num2=3, and operator='+'.
Input first number
num1 = 5.0
Input second number
num2 = 3.0
Input operator
operator = '+'
Check operator and calculate
operator is '+', so result = 5.0 + 3.0 = 8.0
Print result
Output: Result: 8.0
| Step | Variable | Value |
|---|---|---|
| 1 | num1 | 5.0 |
| 2 | num2 | 3.0 |
| 3 | operator | + |
| 4 | result | 8.0 |
Why This Works
Step 1: Input numbers and operator
We use input() to get numbers and operator from the user, converting numbers to float for decimal support.
Step 2: Check operator with if-elif
We use if and elif to decide which arithmetic operation to perform based on the operator.
Step 3: Handle division by zero
Before dividing, we check if the second number is zero to avoid errors and print a friendly message if so.
Step 4: Print the result
Finally, we print the calculated result using print().
Alternative Approaches
def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def divide(a, b): if b == 0: return 'Error: Division by zero' return a / b ops = {'+': add, '-': subtract, '*': multiply, '/': divide} num1 = float(input('Enter first number: ')) num2 = float(input('Enter second number: ')) operator = input('Enter operator (+, -, *, /): ') if operator in ops: result = ops[operator](num1, num2) print('Result:', result) else: print('Invalid operator')
num1 = input('Enter first number: ') num2 = input('Enter second number: ') operator = input('Enter operator (+, -, *, /): ') expression = num1 + operator + num2 try: result = eval(expression) print('Result:', result) except ZeroDivisionError: print('Error: Division by zero is not allowed.') except Exception: print('Invalid input or operator')
Complexity: O(1) time, O(1) space
Time Complexity
The calculator performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a fixed amount of memory for variables and does not grow with input size, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time, but using if-elif is simplest and fastest for beginners, while dictionary mapping adds flexibility with minimal overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-elif statements | O(1) | O(1) | Simple and clear code for beginners |
| Dictionary with functions | O(1) | O(1) | Cleaner code and easy to extend |
| Eval function | O(1) | O(1) | Concise but risky, not safe for untrusted input |