Calculator Program in Python: A Step-By-Step Guide
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
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)
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")
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
Method 3: Creating a GUI calculator using tkinter in Python
#importing tkinter libraryimport tkinter as tkimport tkinter.messageboxfrom 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
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)
p
10 months ago
Report Abuse
Reply to pardeep Mahto