Versatile Python Calculator

Posted by

Python Multipurpose Calculator

Welcome to the Python Multipurpose Calculator!

Python is a versatile programming language that can be used for various applications, including building a calculator. With this multipurpose calculator, you can perform basic arithmetic operations, calculate scientific functions, and even convert units!

Basic Arithmetic Operations

Enter two numbers in the input fields below and select the operation you want to perform:

+

*
/

function calculate() {
let num1 = document.getElementById(“num1”).value;
let num2 = document.getElementById(“num2”).value;
let operation = document.getElementById(“operation”).value;
let result;

if (operation === “add”) {
result = parseFloat(num1) + parseFloat(num2);
} else if (operation === “subtract”) {
result = parseFloat(num1) – parseFloat(num2);
} else if (operation === “multiply”) {
result = parseFloat(num1) * parseFloat(num2);
} else if (operation === “divide”) {
result = parseFloat(num1) / parseFloat(num2);
}

document.getElementById(“result”).innerHTML = “Result: ” + result;
}

Scientific Functions

Want to calculate square roots or trigonometric functions? Just enter a number and select the function:

Square Root
Sine
Cosine
Tangent

function calculateFunction() {
let num3 = document.getElementById(“num3”).value;
let func = document.getElementById(“function”).value;
let functionResult;

if (func === “sqrt”) {
functionResult = Math.sqrt(parseFloat(num3));
} else if (func === “sin”) {
functionResult = Math.sin(parseFloat(num3));
} else if (func === “cos”) {
functionResult = Math.cos(parseFloat(num3));
} else if (func === “tan”) {
functionResult = Math.tan(parseFloat(num3));
}

document.getElementById(“functionResult”).innerHTML = “Result: ” + functionResult;
}

Unit Conversion

Need to convert units such as temperature or length? Just select the units and enter the value:

Celsius to Fahrenheit
Feet to Meters
Pounds to Kilograms

function convertUnits() {
let value = document.getElementById(“value”).value;
let conversion = document.getElementById(“conversion”).value;
let conversionResult;

if (conversion === “celsiusToFahrenheit”) {
conversionResult = (parseFloat(value) * 9/5) + 32;
} else if (conversion === “feetToMeters”) {
conversionResult = parseFloat(value) * 0.3048;
} else if (conversion === “poundsToKilograms”) {
conversionResult = parseFloat(value) * 0.453592;
}

document.getElementById(“conversionResult”).innerHTML = “Result: ” + conversionResult;
}