Python programming is a powerful and adaptable language for building and developing calculators and performing mathematical computations. Its simplicity, versatility, and large library of mathematical functions make it an excellent choice for anyone who is interested in developing custom calculators or work with complex mathematical problems.
A calculator application is a great example of a basic program that can be developed with Python. This calculator application can perform simple arithmetic functions like addition, subtraction, multiplication, and division, as well as more complex functions like calculating the logarithm of a number, as well as exponential or trigonometric operations. Python can be used to build more sophisticated calculators, such as those that perform scientific calculations, graph functions, and solve equations. In Python, we can build a calculator application by using the input() function to accept user input and the print() function to display the output. Conditional statements and loops can also be used to develop a simple user interface and add more functionality for enhancing their usability and customization.
Python can be used for a wide range of other mathematical applications, including data analysis, machine learning, and simulation, in addition to creating calculators. In our project, we worked on building our first calculator, and we used approximately 20 different functions to do so. In the following sections, we will explain the development and implementation processes.
How can calculators carry out specific tasks based on user input? How we'll code our own calculator with Python3!
How can we add more functions to a programming platform?
What are the various types of calculators?
Computer: MacOS, Chromebook
CodeHS Compiler
Internet
Keyboard
Python 3 Programming Language
Our project is to program a functional calculator, in which the user can input the operation they would like the computer to do, then input the two numbers. The computer then does the operation desired, and outputs the result. This is how we made our program.
Open www.codehs.com, or any code editor you know of.
Click Log in, or Sign up if needed.
Write pseudocode/algorithm for the program.
Write the program, iterating as needed.
Test the final program.
To use the program, simply follow these steps:
Click Run in the compiler below.
Type the name of one of the functions we offer.
Type in each number for the computer to use.
The computer will print the answer onscreen.
BACKGROUND RESEARCH
Python is a powerful programming language that is widely used in a variety of applications, including calculators. Users can use Python calculator applications to perform a variety of mathematical operations such as addition, subtraction, multiplication, and division. Python calculator applications can range in features from simple arithmetic operations to more complex mathematical functions such as trigonometric functions, logarithms, and exponentials.Python calculator applications benefit from being easily customizable and extendable. Developers can add new features and functionality to the calculator application by modifying the it. Python calculator applications can also be used in together with other Python-based tools and libraries to build more difficult applications.
Python is a beginner-friendly programming language with simple command syntax and indentation for new lines and nested functions. To be able to script our program, we needed to understand some of the language's syntax. We investigated their application and have provided a sort explanation below.
import math pulls methods from the math library to use in our program. the documentation of available functions in the math module can be found here.
print("Hello World") prints the text in "quotes" on the console.
user_input = input("Enter string: ") prints the string in "quotes" on the console, in which user enters their response. in this case, the response is stored in the variable user_input.
user_int_input = int(input("Enter integer: ")) recieves user input, only for integer values. stored as integer.
variable_string.upper() converts all letters in a string to uppercase. used to make constant the capital variances of user inputs.
if variable_string.upper() == "HELLO": checks if the string "HELLO" is stored in the variable variable_string. lines written and indented under this conditional statement will be excecuted if the condition is true, then continue to run. if not, it will skip over these indented lines.
elif variable_string.upper() == "WORLD": after checking for the first conditional (6), checks if the string "WORLD" is stored in the variable variable_string. lines written and indented under this conditional will run if this condition is met.
else: if, after checking all the conditionals above it, none of them are true, the code indented and written below this line will be excecuted
+ - * / ** % built in operational functions include addition (+), subtraction (-), multiplication (*), division (/), exponentiation (**), modulus (%). the math module has more operations & functions to use.
You can visit our SS Component page for extra information.
SCIENTIFIC PRINCIPLE / ESSENTIAL UNDERSTANDING
#We start with importing the math library, and printing a title onto the screen. We also list the operations that our calculator can compute.
import math
print("Calculator + Converter")
print("(addition, subtraction, multiplication, division, mod, exponentiation)\n")
#We ask the user to type in the operation desired. Our program has the computer store the inputted string as the variable operation.
operation = input("Enter operation: ")
#Then, we have the computer run through a series of decisions. We start with the first conditional. If the user enters "addition" (the capitals don't matter because of .upper()), the computer will ask for two numbers, add them, and print their sum onscreen. If this isn't the case, it will skip the indented commands and move on to the next conditional.
if operation.upper() == "ADDITION":
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
addition = num1 + num2
print(str(num1) + "+" + str(num2) + "=" + str(float(addition)))
#With this conditIonal, the computer checks if the operation is "subtraction", for which it will ask for two numbers, subtract them, and print their difference onscreen. Again, if this isn't the case, it will skip the indented and check for the next conditional.
elif operation.upper() == "SUBTRACTION":
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
subtraction = num1 - num2
print(str(num1) + "-" + str(num2) + "=" + str(float(subtraction)))
#This conditional checks for multiplication, for which it gets two numbers from the user, multiplies them, and prints their product. Otherwise, it goes to the next conditional.
elif operation.upper() == "MULTIPLICATION":
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
multiplication = num1 * num2
print(str(num1) + "*" + str(num2) + "=" + str(float(multiplication)))
#Again, if the user entered division, the computer will ask for two numbers, divide, and print their quotient.
elif operation.upper() == "DIVISION":
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if num2 == 0:
print(str(num1) + "/" + str(num2) + "=" + "undefined")
else:
division = num1 / num2
print(str(num1) + "/" + str(num2) + "=" + str(float(division)))
#If the operation is "mod", we use a function from the math module, fmod(), to calculate the remainder and print it onscreen. If not, it skips the indented code.
elif operation.upper() == "MOD":
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
mod = fmod(num1, num2)
print(str(num1) + "mod" + str(num2) + "≡" + str(float(mod)))
#If the user writes "exponentiation", it will get the base and exponent from the user, calculating and printing the result onscreen.
elif operation.upper() == "EXPONENTIATION":
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
exponentiation = num1 ** num2
print(str(num1) + "^" + str(num2) + "=" + str(float(exponentiation)))
#Continuing with the program, we ask for just one number, find and print the factorialed result if the user asks for factorial.
elif operation.upper() == "FACTORIAL":
num1 = float(input("Enter number: "))
factorial = math.factorial(num1)
print(str(num1) + "!" + "=" + str(int(factorial)))
#Our next conditional finds the absolute value of the number the user gives.
elif operation.upper() == "ABSOLUTE VALUE":
num1 = float(input("Enter number: "))
absolute_value = abs(num1)
print("|" + str(num1) + "|" + "=" + str(float(absolute_value)))
#Then this conditional finds the GCD (Greatest Common Divisor) of two numbers given by the user.
elif operation.upper() == "GCD":
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
gcd = math.gcd(num1, num2)
print("gcd(" + str(num1) + ", " + str(num2) + ") = " + str(float(gcd)))
#Following the same idea, this conditional performs the permutation of two inputted numbers.
elif operation.upper() == "PERMUTATION":
n = int(input("Enter first number: "))
k = int(input("Enter second number: "))
perm = math.perm(n, k)
print(str(n) + " choose " + str(k) + " = " + str(float(perm)))
#The same concept applies to the rest of the functions we created: if the user enters the function's name, it prompts the user for the necessary numbers, performs the operation, and prints the result onscreen. Various operations, such as finding the remainder, logarithm, evaluating the square root, or calculating sin, cos, tan, and inverse trigonometric functions, are possible.
elif operation.upper() == "REMAINDER":
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
rem = math.remainder(num1, num2)
print(str(num1) + "/" + str(num2) + ", R. " + str(rem))
elif operation.upper() == "LOG":
x = float(input("Enter x: "))
base = float(input("Enter base: "))
log = math.log(x, base)
print("log(" + str(x) + ", " + str(base) + ") = " + str(log))
elif operation.upper() == "SQRT":
num1 = float(input("Enter number: "))
sqrt = math.sqrt(num1)
print("sqrt(" + str(num1) + ") = " + str(sqrt))
elif operation.upper() == "ACOS":
num1 = float(input("Enter number (between 0 and pi): "))
acos = math.acos(num1)
print("acos(" + str(num1) + ") = " + str(acos))
elif operation.upper() == "ASIN":
num1 = float(input("Enter number (between -pi/2 and pi/2): "))
asin = math.asin(num1)
print("asin(" + str(num1) + ") = " + str(asin))
elif operation.upper() == "ATAN":
num1 = float(input("Enter number (between -pi/2 and pi/2): "))
atan = math.atan(num1)
print("atan(" + str(num1) + ") = " + str(atan))
elif operation.upper() == "COS":
num1 = float(input("Enter number: "))
cos = math.cos(num1)
print("cos(" + str(num1) + ") = " + str(cos))
elif operation.upper() == "SIN":
num1 = float(input("Enter number: "))
sin = math.sin(num1)
print("sin(" + str(num1) + ") = " + str(sin))
elif operation.upper() == "TAN":
num1 = float(input("Enter number: "))
tan = math.tan(num1)
print("tan(" + str(num1) + ") = " + str(tan))
elif operation.upper() == "ERROR":
num1 = float(input("Enter number: "))
erf = math.erf(num1)
print("erf(" + str(num1) + ") = " + str(erf))
#Finally, if the user entered something that the conditionals don't recognize, it will print "Invalid" onscreen.
else:
print("Invalid")
PROJECT IMPLEMENTATION
Embedded below is our completed calculator! The code is present on the left, and can be run on the right. You can apply our code with help of the run button.
REAL LIFE CONNECTION
Many people believe that using a calculator is unfair. They have a point, but only when it comes to frivolous grade-school tests designed to help students to understand concepts. However, in the real world, where mistakes have consequences, it is best to learn computer science in order to automate many of the precise calculations required.
Such automations speed up and improve the accuracy of extremely complex and high-risk calculations such as rocket trajectory. They say math is everywhere, and calculators convert its logic into numbers. For example, we use our calculator routinely in geometry class to evaluate trivial expressions.
INVESTIATING QUESTIONS
Why do we need calculators and are calculators useful? They are more reliable and less susceptible to human error. They are fast and can help to different purposes. Calculators are useful tools for quickly and accurately performing mathematical operations. They are capable of performing basic arithmetic functions such as addition, subtraction, multiplication, and division, as well as more complex operations such as logarithms, trigonometric functions, and statistical calculations.
Calculators are especially useful in fields where large or complex calculations must be finished fast and precisely, such as engineering, science, finance, and accounting. They can also help students learn computational concepts or anyone who needs to perform basic calculations. Calculators have also been demonstrated to improve mathematical skills and aid in problem-solving by allowing users to check their work and examine solutions.
How can calculators perform specific operations based on user input? Calculators can be programmed to carry out specific operations in response to user input. Microprocessors in modern calculators are programmed to execute specific mathematical methods and functions in response to user input.
When a user enters a calculation into a calculator, the calculator reads the input and processes it according to a set of predefined rules and functions built into the calculator's software. If a user enters "2+2," for example, the calculator's software recognizes this as an addition operation and performs the calculation by adding 2 and 2.They can use conditionals to check for certain phrases that represent names of functions. They are able to work with if/elif/else statements to clarify the user's expectations. if/elif/ else conditions helped in differentiating the user's operation and selecting the appropriate method to implement from the math library.
How can we get more functions than what already available on a programming platform? We can use libraries to import more functions. In this project, we used the math library for more extensive functions. We could use the math library to implement trigonometric functions, as well as power and logarithmic functions. We applied for twenty different mathematical operations.
What different types of calculators are there?
Calculators come in a wide range of styles, from simple handheld devices to complex computer software programs. Here are some of the most common calculators:
Basic calculators: The most common and basic type is basic calculators. It has a few basic functions like as addition, subtraction, multiplication, and division. They are generally small and low-cost devices that are commonly used for simple computations.
Scientific calculators: They have more functions than basic calculators and are intended for use in science (computer science, data science, etc.), engineering, and mathematics. They commonly include trigonometric functions, logarithms, exponents, and statistical functions.
Financial calculators: These calculators are applied in business to calculate , interest rates, loan payments, etc.
Online calculators: These calculators are accessible via a web browser and are offered online. They can range from simple to complex, and they include a broad range of functions and applications.
Graphing calculators are commonly used in high school and college-level math and science courses. Users can graph equations and perform complicated computations with them.
Starting with the basic 4-function calculator, functions expand to scientific, graphing, financial, and statistical calculators. Each of these help people with more field-specific operations. Our calculator was a kind of scientific calculator since it helps to do trigonometric or exponential operations, etc.
CONCLUSION
As a result of this project, we learned and practiced relevant computer science skills. We got to practice problem solving and computational thinking in addition to learning the computer science concepts required for this project. We built a calculator with three inputs: a first number, a second number, and an operation. Then, on the two inputs, it will perform the specified operation and return the result. But it doesn't stop there. There are numerous other operations that could be included. Graphing calculators are also available!
We could try to allow the user to operate on more than two numbers in the future (by asking an additional question and feeding that input into a for loop), or let the user enter entire expressions to evaluate using PEMDAS (which would involve a bunch of if statements, concatenating, the in keyword, the find method, and the method isdigit), or let the user enter the expression by clicking buttons.
In conclusion, Python is a versatile and widely used programming language that is ideal for building a calculator application. Python's clear and brief syntax makes it simple to write understandable and maintainable code. Developing a calculator application in Python is a beneficial way to learn programming fundamentals such as variables, data types, and conditional statements. Furthermore, Python includes a number of libraries and modules that can be used to build a more sophisticated calculator application capable of performing advanced mathematical computations.
Additionally, Python's connectivity with a wide range of platforms and systems makes it simple to use a calculator application across multiple platforms, including desktops, web browsers, and mobile devices. In summary, the flexibility and ease of use of Python make it an excellent choice for developing a calculator application, and it can also serve as a foundation for more complex projects in the future. Python is a valuable language to learn for any aspiring programmer due to its wide range of applications.
VIDEO I
ACTION VIDEO
REFERENCES
https://www.hp.com/us-en/shop/tech-takes/top-3-uses-for-financial-calculator
https://www.hp.com/us-en/shop/tech-takes/top-5-uses-for-a-scientific-calculator
https://www.livescience.com/14087-calculators-calculate.html
https://www.thecalculatorsite.com/articles/units/history-of-the-calculator.php
https://www.tutorialsteacher.com/python/python-version-history