How to Create Basic Calculator in Python Tutorial

In this tutorial, we will create a "How to Create a Basic Calculator in Python". The purpose of this tutorial is to teach you the process of building a calculator. We will try to understand the steps involved in making this calculator. I will provide a sample program to demonstrate the actual coding of this tutorial.

This tutorial is simple and easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program give you the basic functionality of the calculator app. I will do my best to provide you with the most simplified method of creating this program, called "Basic Calculator". So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python

Creating Main Function

This is the main function of the application. The following code will display the GUI of calculator in terminal console . To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def add(x, y):
  2. return x + y
  4. def subtract(x, y):
  5. return x - y
  7. def multiply(x, y):
  8. return x * y
  10. def divide(x, y):
  11. return x / y
  13. while True:
  14. print("\n\n========== Basic Calculator ==========\n\n")
  15. print("Choose an operation: ")
  16. print("1.Add")
  17. print("2.Subtract")
  18. print("3.Multiply")
  19. print("4.Divide")
  21. choice = input("Enter your choice: ")
  24. if choice in ('1', '2', '3', '4'):
  25. try:
  26. num1 = float(input("Enter first number: "))
  27. num2 = float(input("Enter second number: "))
  28. except ValueError:
  29. print("Invalid input. Please enter a number.")
  30. continue
  32. if choice == '1':
  33. print(num1, "+", num2, "=", add(num1, num2))
  35. elif choice == '2':
  36. print(num1, "-", num2, "=", subtract(num1, num2))
  38. elif choice == '3':
  39. print(num1, "*", num2, "=", multiply(num1, num2))
  41. elif choice == '4':
  42. print(num1, "/", num2, "=", divide(num1, num2))
  45. next_calculation = input("Try another calculation? (yes/no): ")
  46. if next_calculation == "no":
  47. break
  48. else:
  49. print("Invalid Input")

In the code that I provided, we first created several functions to handle operations for our calculator, such as adding and subtracting numbers.

Then, we proceed to enclose the entire code with a while loop to ensure the continuation of the program. To get the result, we declare the operation function that appends the two numbers to it.


The How to Create Basic Calculator in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Basic Calculator in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment