# Creating a scientific calculator with Python

A scientific calculator is a type of calculator that allows users to perform advanced mathematical calculations. While Python has a built-in `math` library that provides access to many mathematical functions, creating a program that can perform complex calculations is not always a simple task. Fortunately, Python makes it easy to create a scientific calculator that can handle everything from basic arithmetic to advanced trigonometric functions.

The scientific calculator in Python that we will create in this post will have the following features:

• Subtraction
• Multiplication
• Division
• Exponentiation
• Square Root
• Logarithm
• Sine
• Cosine
• Tangent

We will use a simple command line interface to prompt the user to input the desired operation and the required values. The program will then perform the calculation and display the result.

Creating a Scientific Calculator in Python

To create a scientific calculator in Python, we will use the `math` library to perform advanced mathematical calculations. We will define a function `scientific_calculator()` that will display a menu of available operations and prompt the user to input a choice. Depending on the user’s choice, the function will perform the corresponding operation using Python’s built-in `math` library and print the result.

``````import math

def scientific_calculator():
print('Scientific Calculator\n')
print('2. Subtraction')
print('3. Multiplication')
print('4. Division')
print('5. Exponentiation')
print('6. Square Root')
print('7. Logarithm')
print('8. Sine')
print('9. Cosine')
print('10. Tangent')
print('11. Exit')

while True:
try:
choice = int(input('\nEnter your choice: '))
if choice == 1:
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
print('Result: ', num1 + num2)
elif choice == 2:
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
print('Result: ', num1 - num2)
elif choice == 3:
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
print('Result: ', num1 * num2)
elif choice == 4:
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
print('Result: ', num1 / num2)
elif choice == 5:
num1 = float(input('Enter base: '))
num2 = float(input('Enter exponent: '))
print('Result: ', num1 ** num2)
elif choice == 6:
num = float(input('Enter a number: '))
print('Result: ', math.sqrt(num))
elif choice == 7:
num = float(input('Enter a number: '))
print('Result: ', math.log10(num))
elif choice == 8:
num = float(input('Enter a number in degrees: '))
elif choice == 9:
num = float(input('Enter a number in degrees: '))
elif choice == 10:
num = float(input('Enter a number in degrees: '))
elif choice == 11:
break
else:
print('Invalid choice. Please choose a number from 1 to 11.')
except ValueError:
print('Invalid input. Please enter a number.\n')``````

In this program, we use a while loop to keep prompting the user for choices until they choose to exit. We use the `try-except` block to handle any invalid inputs that the user might provide.

The `math` library provides functions to perform advanced mathematical calculations like square root, logarithm, and trigonometric functions. In the code, we use `math.sqrt()` to calculate the square root of a number, `math.log10()` to calculate the logarithm with base 10 of a number, and `math.sin()`, `math.cos()`, and `math.tan()` to calculate the sine, cosine, and tangent of an angle in radians.

This code defines a function `scientific_calculator()` that displays a menu of available operations and prompts the user to enter a choice. Depending on the user’s choice, the function performs the corresponding operation using Python’s built-in math library and prints the result.

You can call the `scientific_calculator()` function in your Python code to run the calculator. For example:

``scientific_calculator()``

This will display the menu and allow the user to perform the desired calculations.

Output

Here is an example of what the console output might look like for some of the operations:

``````Scientific Calculator

2. Subtraction
3. Multiplication
4. Division
5. Exponentiation
6. Square Root
7. Logarithm
8. Sine
9. Cosine
10. Tangent
11. Exit

Enter first number: 10
Enter second number: 20
Result:  30.0

Enter a number: 25
Result:  5.0

Enter a number in degrees: 45
Result:  0.7071067811865476``````

In this example, the user first chooses to perform addition, then enters the numbers 10 and 20. The program performs the addition and displays the result, which is 30.0. Then, the user chooses to calculate the square root of 25, and the program correctly displays the result, which is 5.0. Finally, the user chooses to calculate the cosine of 45 degrees, and the program correctly displays the result, which is 0.7071067811865476.

Conclusion

This is how you create a simple scientific calculator in Python using the `math` library. We used a command line interface to prompt the user for inputs and performed calculations based on the user’s choices. This scientific calculator can be expanded upon with more functions and features, making it a useful tool for engineers, scientists, and mathematicians.