Python Simple Calculator with Square Root
Introduction
This guide shows how to create a simple calculator in Python that can perform basic arithmetic operations including square roots. Python is a versatile programming language that's great for beginners and experienced developers alike.
Whether you're learning Python or need a quick calculation tool, this calculator implementation will help you understand how to handle mathematical operations programmatically.
This calculator uses Python's built-in math.sqrt() function for accurate square root calculations.
How to Use the Calculator
The interactive calculator on the right allows you to test different calculations. Simply enter two numbers and select an operation, then click "Calculate".
The calculator supports these operations:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Square Root (√)
For square root operations, only the first number input is used. The second number input will be ignored.
Python Code Implementation
Here's the complete Python code for the calculator:
import math
def simple_calculator():
print("Simple Calculator")
print("Operations:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Square Root (√)")
while True:
try:
num1 = float(input("Enter first number: "))
operation = input("Enter operation (1/2/3/4/5): ")
if operation != '5':
num2 = float(input("Enter second number: "))
if operation == '1':
print(f"Result: {num1} + {num2} = {num1 + num2}")
elif operation == '2':
print(f"Result: {num1} - {num2} = {num1 - num2}")
elif operation == '3':
print(f"Result: {num1} * {num2} = {num1 * num2}")
elif operation == '4':
if num2 == 0:
print("Error: Division by zero")
else:
print(f"Result: {num1} / {num2} = {num1 / num2}")
elif operation == '5':
if num1 < 0:
print("Error: Square root of negative number")
else:
print(f"Result: √{num1} = {math.sqrt(num1)}")
else:
print("Invalid operation")
another = input("Calculate another? (yes/no): ")
if another.lower() != 'yes':
break
except ValueError:
print("Invalid input. Please enter numbers only.")
simple_calculator()
The code includes error handling for invalid inputs and division by zero. The square root function uses Python's math.sqrt() which returns a floating-point number.
Examples
Example 1: Addition
Input:
- First number: 5
- Operation: 1 (Addition)
- Second number: 3
Output:
Result: 5 + 3 = 8
Example 2: Square Root
Input:
- First number: 25
- Operation: 5 (Square Root)
Output:
Result: √25 = 5.0
Example 3: Division by Zero
Input:
- First number: 10
- Operation: 4 (Division)
- Second number: 0
Output:
Error: Division by zero
FAQ
Can I use this calculator for complex numbers?
No, this calculator only handles real numbers. For complex number operations, you would need to use Python's cmath module.
How accurate are the square root calculations?
The square root calculations use Python's built-in math.sqrt() function which provides accurate results for real numbers.
Can I modify this calculator to include more operations?
Yes, you can easily extend this calculator by adding more conditions in the if-elif-else block and implementing the corresponding operations.