Calculator Program in Python: A Step-By-Step Guide

Calculator Program in Python: A Step-By-Step Guide

1 min read5.7K Views 1 Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Sep 13, 2024 18:09 IST

Looking for a powerful and user-friendly calculator program in Python? In this blog we will discuss three different methods for calculator program in python.

2023_03_MicrosoftTeams-image-201.jpg

A calculator performs arithmetic operations along with solving equations, exponential and trigonometric operations.

But what if we told you that you can create a calculator program in Python yourself!

If you know basic Python programming, we will show you to create a graphical user interface from scratch that can perform basic arithmetic operations. 
So, without further delay, let’s get started.

Also, explore Conditional Statements in Python

Method -1: Simple Calculator Program in Python


 
# Basic Calculations
# Calculator Program in Python by using input() and format() functions
#Promting input from the user
n1 = float(input("Enter the First Number: "))
n2 = float(input("Enter the Second Number: "))
#addition
print("{} + {} = ".format(n1, n2))
print(n1 + n2)
#subtraction
print("{} - {} = ".format(n1, n2))
print(n1 - n2)
#multiplication
print("{} * {} = ".format(n1, n2))
print(n1 * n2)
#division
print("{} / {} = ".format(n1, n2))
print(n1 / n2)
Copy code

Output

Explanation: In the above Program, we take two numeric values (here, we are taking float data type), and we get all the outputs (addition, subtraction, multiplication, and division) once we input both numbers.

Also Read: String Formatting in Python

Also Read: Python Input Function

From nerd to expert ninja! Unlock Python on a deeper level and explore the best Python programmes from top colleges and online Python courses with our detailed guide.

Method 2: Calculator Program in Python using function


 
#Calculator Program in python by defining operations
# Define Operators or Functions: Addition, Subtraction, Multiplication, and Division
# Addition
def addition(n1, n2):
return n1 + n2
# Subtraction
def subtraction(n1, n2):
return n1 - n2
# Multiplication
def multiplication(n1, n2):
return n1 * n2
# Division
def division(n1, n2):
return n1 / n2
print("Select Operations")
print(
"1. Addition\n"\
"2. Subtraction\n"\
"3. Multiplication\n"\
"4. Division\n")
# Giving the option to the user to choose the operation
operation = int(input("Enter choice of operation 1/2/3/4: "))
#Taking Input from the Users
n1 = float(input("Enter the First Number: "))
n2 = float(input("Enter the Second Number: "))
# Apply Conditional Statements: To make operation as-per-user choices
if operation == 1:
print (n1, "+", n2, "=", addition(n1, n2))
elif operation == 2:
print (n1, "-", n2, "=", subtraction(n1, n2))
elif operation == 3:
print (n1, "*", n2, "=", multiplication(n1, n2))
elif operation == 4:
print (n1, "/", n2, "=", division(n1, n2))
else:
print("Invalid Input")
Copy code

Output

Explanation:

The above Program is created in three steps:

Step -1: Define Functions: Addition, Subtraction, Multiplication, and Division

Step-2: Promoting Input from the user

(i). Choosing which operation to perform

(ii). Enter the first and second numbers (float data type)

Step-3:  Apply Conditional Statements: To make operation as-per-user choices

Must Read: def keyword in Python

Must Read: Data Type in Python

Conditional Statements in Python – Python Tutorial
Conditional Statements in Python – Python Tutorial
A conditional statement as the name suggests itself, is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions encountered by...read more
Flow Control in Python – Explore Conditional Programming, Python Loops and Statements
Flow Control in Python – Explore Conditional Programming, Python Loops and Statements
Python programming language normally executes the instructions one after the another, until you say otherwise. However, the real strength of programming isn’t just for the programs to start from the...read more
For Loop in Python (Practice Problem) – Python Tutorial
For Loop in Python (Practice Problem) – Python Tutorial
For loops in Python are designed to repeatedly execute the code block while iterating over a sequence or an iterable object such as a list, tuple, dictionary, or set. This...read more

Method 3: Creating a GUI calculator using tkinter in Python


 
#importing tkinter library
import tkinter as tk
import tkinter.messagebox
from tkinter.constants import SUNKEN
window=tk.Tk()
window.title('My Calci')
frame=tk.Frame(master=window,bg="skyblue",padx=10)
frame.pack()
entry=tk.Entry(master=frame,relief=SUNKEN,borderwidth=3,width=30)
entry.grid(row=0,column=0,columnspan=4,ipady=2,pady=2)
#define myclick function to get the input from the user
def myclick(number):
entry.insert(tk.END,number)
#define an equal function to evaluate the value
# try-except is use to handle the error
def equal():
try:
y=str(eval
Copy code
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio

Comments

(1)

I want to ask two python program. 1st : develop a python programme that function as simple calculator which allows the user to enter two numbers perform basic mathematical operation namely addition subtraction multiplication division and display the result 2nd : develop a python programme that can

Reply to pardeep Mahto