Calculator Program in Python
Calculator Program in Python
Vikram Singh
Assistant Manager - Co ntent
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.
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
#addition
#subtraction
#multiplication
#division
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.
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.
Copy code
# Addition
# 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
# Division
operat ion = int (input ("Ent er choice of operat ion 1/2/3/4: "))
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))
else:
print ("Invalid Input ")
Output
Explanation:
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)
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
def myclick(number):
ent ry.insert (t k.END,number)
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.