 # Python Program to Make a Simple Calculator

In Python, we can create a simple python calculator that, based on user input, can do fundamental arithmetic operations such as addition, subtraction, multiplication, and division. We can either create a simple calculator using the if-elif statements and an advanced calculator as well. Source Code

## How do you make a Calculator in Python?

To understand how to make a python calculator, look at the algorithm given below:

1. Initialise the two numbers.
2. Ask the user to enter an option.
3. After receiving the user’s option, create if conditions for each operation based on the option.
4. Carry out the required action.
5. Print the result.

## How do you do an Advanced Calculator in Python?

This advanced python calculator also works with text statements. The user is not required to give algebraic expressions all of the time. It extracts the word from the user’s command and then formulates the expression.

### Source Code:

                    
def extract_from_text(text):

l=[]

for t in text.split(' '):

try:

l.append(float(t))

except ValueError:

pass

return l

# calculating LCM

def lcm(x,y):

L=x if x>y else y

while L<=x*y:

if L%x==0 and L%y==0:

return L

L+=1

# calculating HCF

def hcf(x,y):

H=x if x=1:

if x%H==0 and y%H==0:

return H

H-=1

return x+y

# Subtraction

def sub(x,y):

return x-y

# Multiplication

def mul(x,y):

return x*y

# Division

def div(x,y):

return x/y

# Remainder

def mod(x,y):

return x%y

# Response to command

# printing - "Thanks for enjoy with me" on exit

def end():

print(response)

input('press enter key to exit')

exit()

def myname():

print(response)

def sorry():

print(response)

# Operations - performed on the basis of text tokens

'SUB':sub,'SUBTRACT':sub, 'MINUS':sub,

'DIFFERENCE':sub,'LCM':lcm,'HCF':hcf,

'PRODUCT':mul, 'MULTIPLY':mul,'MULTIPLICATION':mul,

'DIVISION':div,'MOD':mod,'REMANDER'

:mod,'MODULAS':mod}

# commands

commands={'NAME':myname,'EXIT':end,'END':end,'CLOSE':end}

print('--------------'+response+'------------')

print('--------------'+response+'--------------------')

while True:

print()

for word in text.split(' '):

if word.upper() in operations.keys():

try:

l = extract_from_text(text)

r = operations[word.upper()] (l,l)

print(r)

except:

print('something went wrong going plz enter again !!')

finally:

break

elif word.upper() in commands.keys():

commands[word.upper()]()

break

else:

sorry()

Output:

enter your queries: tell me the LCM of 18,8

72



## How do you make a Calculator using if else in Python?

### Example: Simple Calculator by Using Functions

                    

return a + z

# This function takes two numbers and subtracts them.

def subtract(a, z):

return a - z

# This function takes two numbers and multiplies them.

def multiply(a, z):

return a * z

# This function takes two numbers and divides them.

def divide(a, z):

return a / z

print("Select operation.")

print("2.Subtraction")

print("3.Multiplication")

print("4.Division")

while True:

# Take input from the user

choice = input("Enter choice(1/2/3/4): ")

# Check to see if your choice is one of the four options

if choice in ('1', '2', '3', '4'):

numb1 = float(input("Enter first number: "))

numb2 = float(input("Enter second number: "))

if choice == '1':

print(numb1, "+", numb2, "=", add(numb1, numb2))

elif choice == '2':

print(numb1, "-", numb2, "=", subtract(numb1, numb2))

elif choice == '3':

print(numb1, "*", numb2, "=", multiply(numb1, numb2))

elif choice == '4':

print(numb1, "/", numb2, "=", divide(numb1, numb2))

break

else:

print("Invalid Input")

Output

Select operation.

2.Subtract

3.Multiply

4.Divide

Enter choice(1/2/3/4): 3

Enter first number: 80

Enter second number: 34

80 * 34 = 2720.0



## How do I import a Calculator into Python?

To import a calculator in Python, follow the steps given below:

Step 1: Importing the required modules, including the Tkinter module

Step 2: We’re going to make a window for our calculator.

Step 3:Creating button designs

Step 4: The buttons’ functions are being mapped out.

Share with friends

## Customize your course in 30 seconds

##### Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.  Ashhar Firdausi
IIT Roorkee
Biology  Dr. Nazma Shaik
VTU
Chemistry  Gaurav Tiwari
APJAKTU
Physics
Get Started

## Browse

##### Python Examples Subscribe
Notify of