Make Calculator in Python
Want to make a Python calculator? In this tutorial, we show you how to code your own!
As a high-level programming language, Python is perfect for this kind of coding project. With a few easy steps, you can create a simple calculator with confidence.
Ready to learn more? Let's get started!
Why Make a Simple Calculator?
Python programming is a welcoming way to learn how to code for kids ages 8-18.
You can create a basic calculator to perform arithmetic operations, including addition, subtraction, multiplication, and division. This is a great approach for learning the basics of the programming language (and preparation for more advanced coding).
While exploring concepts like conditionals, while loops, and more, young coders can improve their overall coding abilities. It also provides practice with numbers, mathematics, and command-line programming.
This type of coding project opens doors to many possibilities. From video games to mobile apps to artificial intelligence, an aspiring coder can get a head start on the many exciting careers and hobbies of the future!
What you Need to Begin
Python is an open source programming language. It's free to everyone!
If you don't already have it, you can download Python 3 from the official website. It works on various operating systems (including Windows, MacOS, and Linux). The download process also includes a link to handy Python documentation.
New to Python? Check out the following resources:
Python Coding 101 (kids age 11+)
Create Virtual Zoom Background (beginner)
Draw the Solar System (beginner graphics)
Looking for something more advanced? Here are some Python tutorials:
Rock Paper Scissors Game (intermediate)
Make a Pet Simulator (intermediate)
How to Graph in Python (intermediate to advanced)
Making your own Python program is fun, but it can be a little intimidating at first. Don't worry! With practice & proper guidance, you'll get the hang of it.
Python Program Terms
Before we begin the Python tutorial, let's take a look at terms and concepts!
Python offers many ways to develop a Graphical User Interface (or GUI). A GUI provides a user different windows, buttons, and other widgets to interact with an application.
Tkinter (along with PyQT and wxPython) is one of the most popular GUIs. Tkinter makes it easy & fast to create various applications, including a GUI calculator.
Along with mainloop—this method allows Tkinter to start running an application—it makes it possible to create a GUI calculator. Once you practice how to make a simple calculator program, you can level up your coding skills with a GUI version!
Def and Elif
Def is the name that defines a function. This is followed by function parameters, which are placed in parentheses. After that, a colon is placed in the line of code. The colon represents the beginning of the function body.
Elif (short for 'else if') allows a user to check for multiple expressions. For instance, if the condition for 'if' is True, it checks other elif blocks. If all other conditions are True, then the 'else' portion is executed.
More Python Terms
- Algorithm: Instructions performed to find the solution to a particular problem.
2. Conditional statement: Used when a given condition is either true or false.
3. Eval: Function that evaluates an expression and returns it as a whole number.
4. Invalid input: Indicates info from user that is not accepted.
5. Lambda: A single-line function that reduces the number of lines of code.
6. Num: Short for number data types, which store numeric values.
7. Recursion: The act of a function to call itself.
8. Source code: Code generated by a programmer.
9. User input: Info provided by the user through input devices like a keyboard.
10. While loop: Repeatedly performs statements until a given condition is satisfied.
For more definitions, check out the Python glossary.
Calculator Program Tutorial
Time to make your own Python program!
There are several ways to create a calculator. Today, we'll focus on a simple calculator to add, subtract, multiple, and divide.
Take a look at the above code. In this example, it allows you to choose two numbers for each function. These numbers are represented by num1 and num2.
As you can see, there are four functions represented by their own groups of code.
The first number (num1) can be added, subtracted, multiplied or divided by the second number (num2) via our Python calculator program.
This next portion of code allows for user input. In this example, it's composed of elif function branching that lets you choose one of the four math operations.
'1' represents addition, '2' represents subtraction, '3' represents multiplication, and '4' represents division.
Once you've selected an operation, enter the two numbers that represent your math problem. For example, if you want to find the answer to 15 x 10, input 15 as your 1st number (num 1) and 10 as your 2nd number (num2).
When all conditions are met for a specific operation—in this case, multiplication—then the body of 'else' is executed, resulting in the printing of the answer: 150.
Note: This equation uses whole numbers (known as integers). Also, we aren't using decimal points. This is due to the int() function in the code. It can be adjusted to accommodate decimals by using the float() function.
Keep in mind that if you enter something invalid, like letters, the application will prompt you with an 'Invalid input' statement. In the above example, we entered ‘math’ instead of a number value. This results in a ‘ValueError’ message.
You've just learned how to make your very own calculator program in Python. Keep practicing and learning new techniques. Before you know it, you'll be able to build your own games and apps. Keep up the good work!
Want to keep learning? Need Help?
We hope you enjoyed this Python programming tutorial!
Creating your own basic calculator is fun & rewarding. Another great way to learn new coding skills is with the help of a friendly & knowledgeable instructor.
At Juni Learning, we offer coding courses for kids. Your student can make their own coding projects with tailored 1:1 guidance.
Have questions? Speak with a Juni Advisor by calling (650) 263-4306 or emailing firstname.lastname@example.org to learn more about our coding curriculum and our approach to online learning.