0% found this document useful (0 votes)
2 views7 pages

Calculator Program in Python

This document provides a step-by-step guide to creating a calculator program in Python, covering three methods: a simple calculator using basic arithmetic, a function-based calculator, and a graphical user interface (GUI) calculator using Tkinter. Each method includes code snippets and explanations for implementing addition, subtraction, multiplication, and division operations. The guide is aimed at individuals with basic Python knowledge looking to enhance their programming skills.

Uploaded by

shailenderojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Calculator Program in Python

This document provides a step-by-step guide to creating a calculator program in Python, covering three methods: a simple calculator using basic arithmetic, a function-based calculator, and a graphical user interface (GUI) calculator using Tkinter. Each method includes code snippets and explanations for implementing addition, subtraction, multiplication, and division operations. The guide is aimed at individuals with basic Python knowledge looking to enhance their programming skills.

Uploaded by

shailenderojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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

Vikram Singh
Assistant Manager - Co ntent

Updated on Dec 29, 2023 16:53 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.

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

Met hod -1: Simple Calculat or Program in Pyt hon

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code

# Basic Calculations
# Calculator Program in Python by using input() and format() functions

#Promting input from the user

n1 = f loat (input ("Ent er t he First Number: "))


n2 = f loat (input ("Ent er t he Second Number: "))

#addition

print ("{} + {} = ".f ormat (n1, n2))


print (n1 + n2)

#subtraction

print ("{} - {} = ".f ormat (n1, n2))


print (n1 - n2)

#multiplication

print ("{} * {} = ".f ormat (n1, n2))


print (n1 * n2)

#division

print ("{} / {} = ".f ormat (n1, n2))


print (n1 / n2)

Output

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
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.

Met hod 2: Calculat or Program in Pyt hon using f unct ion

Copy code

#Calculator Program in python by defining operations

# Define Operators or Functions: Addition, Subtraction, Multiplication, and Division

# Addition

def addit ion(n1, n2):


ret urn n1 + n2

# Subtraction

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
def subt ract ion(n1, n2):
ret urn n1 - n2

# Multiplication

def mult iplicat ion(n1, n2):


ret urn n1 * n2

# Division

def division(n1, n2):


ret urn n1 / n2

print ("Select Operat ions")


print (
"1. Addit ion\n"
"2. Subt ract ion\n"
"3. Mult iplicat ion\n"
"4. Division\n")

# Giving the option to the user to choose the operation

operat ion = int (input ("Ent er choice of operat ion 1/2/3/4: "))

#Taking Input from the Users

n1 = f loat (input ("Ent er t he First Number: "))


n2 = f loat (input ("Ent er t he Second Number: "))

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

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
if operat ion == 1:
print (n1, "+", n2, "=", addit ion(n1, n2))

elif operat ion == 2:


print (n1, "-", n2, "=", subt ract ion(n1, n2))

elif operat ion == 3:


print (n1, "*", n2, "=", mult iplicat ion(n1, n2))

elif operat ion == 4:


print (n1, "/", n2, "=", division(n1, n2))

else:
print ("Invalid Input ")

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

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
(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 T ype in Python

Condit ional St at ement s in Pyt hon – Pyt hon Tut orial


A co nditio nal statement as the name suggests itself, is used to handle
co nditio ns in yo ur pro gram. These statements guide the pro gram while making
decisio ns based o n the co nditio ns enco untered by...re ad m o re

Flow Cont rol in Pyt hon – Explore Condit ional…


Programming, Pyt hon Loops and St at ement s
Pytho n pro gramming language no rmally executes the instructio ns o ne after the
ano ther, until yo u say o therwise. Ho wever, the real strength o f pro gramming isn’t just fo r the pro grams to
start fro m the...re ad m o re

For Loop in Pyt hon (Pract ice Problem) – Pyt hon Tut orial…
Shiksha Online
Fo r lo o ps in Pytho n are designed to repeatedly execute the co de blo ck while
iterating o ver a sequence o r an iterable o bject such as a list, tuple, dictio nary, o r set. This...re ad m o re

Met hod 3: Creat ing a GUI calculat or using t kint er in Pyt hon

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.
Copy code

#importing tkinter library


import t kint er as t k
import t kint er.messagebox
f rom t kint er.const ant s import SUNKEN

window=t k.T k()


window.t it le('My Calci')
f rame=t k.Frame(mast er=window,bg="skyblue",padx=10)
f rame.pack()
ent ry=t k.Ent ry(mast er=f rame,relief =SUNKEN,borderwidt h=3,widt h=30)
ent ry.grid(row=0,column=0,columnspan=4,ipady=2,pady=2)

#define myclick function to get the input from the user

def myclick(number):
ent ry.insert (t k.END,number)

#define an equal function to evaluate the value


# try-except is use to handle the error

def equal():
t ry:
y=st r(eval

Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 30 -Dec-20 23.

You might also like