Calculator Program in Python: Build a Simple Calculator

Calculator Program in Python: Build a Simple Calculator

Calculator Program in Python: Build a Simple Calculator

Python is a versatile programming language that makes it easy to perform various operations. One of the most common applications for beginners is building a simple calculator that can handle basic arithmetic operations like addition, subtraction, multiplication, and division. In this tutorial, we will guide you through creating a basic calculator program using Python. We will also explore different methods to implement it, such as using if-else statements and functions.


Table of Contents

  • Introduction
  • Input and Output Format
  • Prerequisite Knowledge
  • Algorithm for Building a Simple Calculator
  • Method 1: Simple Calculator Using Nested If-Else Statements
  • Method 2: Calculator Program Using Functions
  • Conclusion

Introduction to the Simple Calculator in Python

Calculator program in Python

A calculator is one of the first programs that many beginners write when learning a new programming language. In Python, building a simple calculator involves accepting input from the user and performing one of the four basic arithmetic operations. This program will enable you to practice using conditional statements and functions, two essential concepts in Python.


Input and Output Format

Before diving into the implementation, let’s define how the input and output will look for our simple calculator.

Input Format:

  1. The first line of input corresponds to the choice of operation (addition, subtraction, multiplication, or division).
  2. The second line will consist of two integers on which the chosen operation will be performed.

Output Format:

  • The program should display the result of the operation.

Prerequisite Knowledge

Before we begin, ensure you’re comfortable with the following Python concepts:

  • If-Else Statements: These are used to make decisions based on conditions. The calculator will depend heavily on this concept.
  • Functions (for method 2): A function allows you to group code that performs a specific task, which can be reused.

Algorithm for Building a Simple Calculator

Let’s break down the steps for building a simple calculator in Python:

  1. Get input from the user: Prompt the user to choose an operation and provide two integers for the calculation.
  2. Perform the operation: Based on the user’s choice, the program will execute the appropriate arithmetic operation.
  3. Print the output: The result of the operation will be displayed to the user.
  4. End the program: Once the result is shown, the program will terminate.

Method 1: Simple Calculator Using Nested If-Else Statements

One way to build a simple calculator in Python is by using nested if-else statements. This method involves checking the user’s choice and performing the corresponding operation.

Here’s how you can implement it:

python

# Python program for a simple calculator using nested if statements

choice = int(input(“Enter your choice:\n”)) # User’s choice (1, 2, 3, 4)

if (choice >= 1 and choice <= 4):
print(“Enter two numbers: “)
num1 = int(input())
num2 = int(input())

if choice == 1: # To add two numbers
res = num1 + num2
print(“Result = “, res)

elif choice == 2: # To subtract two numbers
res = num1 – num2
print(“Result = “, res)

elif choice == 3: # To multiply two numbers
res = num1 * num2
print(“Result = “, res)

elif choice == 4: # To divide two numbers
res = num1 / num2
print(“Result = “, res)
else:
print(“Wrong input…!!”)

Input Example:

mathematica
Enter your choice:
3
Enter two numbers:
9
5

Output:

makefile
Result = 45

This method checks the user’s choice and performs the selected operation. However, if the user inputs an invalid choice, the program will print a “Wrong input” message.


Method 2: Calculator Program Using Functions

An alternative approach to building the calculator is by utilizing functions. This method makes the code more modular and reusable. You can define a function to handle the operations and call it when needed.

Here’s the implementation:

python
# Function to perform calculations
def calculate():
print("Select an operator (+, -, *, /):")
operation = input() # User's choice of operator
print(“Enter two numbers:”)
number_1 = int(input())
number_2 = int(input())

if operation == ‘+’: # Add the two numbers
print(number_1 + number_2)

elif operation == ‘-‘: # Subtract the two numbers
print(number_1 – number_2)

elif operation == ‘*’: # Multiply the two numbers
print(number_1 * number_2)

elif operation == ‘/’: # Divide the two numbers
if number_2 != 0:
print(number_1 / number_2)
else:
print(“Error: Division by zero”)
else:
print(“Invalid Input”)

# Call the function
calculate()

Input Example:

mathematica
Enter your choice:
+
Enter two numbers:
25
5

Output:

 
30

In this method, the user is prompted to select an operation and enter the numbers, and the program performs the corresponding operation based on the input. The function approach is more efficient for extending or modifying the calculator in the future.


Conclusion

Building a simple calculator in Python is an excellent way to practice key programming concepts such as if-else statements and functions. In this tutorial, we’ve covered two different approaches to creating a calculator:

  1. Using nested if-else statements for handling operations.
  2. Using functions to make the code more modular and reusable.

Whether you prefer a direct conditional approach or a more structured function-based design, both methods will help you build your skills and improve your Python programming abilities.

 

Click Here to know more our program!

Calculator program in Python

 


 

c