In this post we learn how to make simple calculator using python with very basic explanations and examples.
this python program is considered as mini project for beginners but before writing code we should know about..
 ifelse.
 function in python.
 while loop.
 for loop.
 Dictionary in python
Now let’s write a python program to make simple calculator.
Simple calculator
Source code:
def add(a,b):
return a+b
def sub(a,b):
return ab
def mul(a,b):
return a*b
def div(a,b):
return a/b
operator = {
'+':add,
'':sub,
'*':mul,
'/':div
}
num1 = float(input('Enter a first number: '))
should_continue = False
while not should_continue:
for keys in operator:
print(keys)
operation = input('Please select a operation: ')
num2 = float(input('Enter next number : '))
calculation = operator[operation]
c1 = calculation(num1,num2)
print(f"{num1} {operation} {num2} = {c1}")
con = input(f"do you want to contineus calculation with {c1} answer? type 'yes' or 'no' ")
if con == 'no':
should_continue = True
else:
num1 = c1
Output:
Enter a first number: 5
+

*
/
Please select a operation: *
Enter next number : 5
5.0 * 5.0 = 25.0
do you want to contineus calculation with 25.0 answer? type 'yes' or 'no' : yes
+

*
/
Please select a operation: +
Enter next number : 15
25.0 + 15.0 = 40.0
do you want to contineus calculation with 40.0 answer? type 'yes' or 'no' : yes
+

*
/
Please select a operation: 
Enter next number : 5
40.0  5.0 = 35.0
do you want to contineus calculation with 35.0 answer? type 'yes' or 'no' : yes
+

*
/
Please select a operation: /
Enter next number : 7
35.0 / 7.0 = 5.0
do you want to contineus calculation with 5.0 answer? type 'yes' or 'no' : no
Explanations
For effective explanation we break above program in some parts and please do not run this parts of program may you get error.
def add(a,b):
return a+b
def sub(a,b):
return ab
def mul(a,b):
return a*b
def div(a,b):
return a/b
NOTE: all bold letter words are variable names or functions.
This four function (name as add , sub , mul , div) is use for performing operation like addition, substraction, multiplication, division etc (this is core part of calculator).
operator = {
'+':add,
'':sub,
'*':mul,
'/':div
}
Here we create a dictionary name as operator, key of dictionary –> is symbols of operations that we do and value of dictionary –> is name similar to our above function (add,sub,mul,div).
num1 = float(input('Enter a first number: '))
should_continue = False
num1 variable is use to take input of first number from user and should_continue variable is use to run a loop.
while not should_continue:
for keys in operator:
print(keys)
operation = input('Please select a operation: ')
while loop is main loop in this program and run until should_continue is equal to True.
for loop is use to print keys of operator dictionary.
operation variable take intput as operator (+ – * /) from user .
num2 = float(input('Enter next number : '))
calculation = operator[operation]
c1 = calculation(num1,num2)
print(f"{num1} {operation} {num2} = {c1}")
num2 variable is use to take input of second numbers from user.
calculation variable is store values of operator dictionary and as we know previously values of operator dictionary is same as function name so basically calculation variable store name of function.
So now calculation variable act as function (let’s understand this concept with example).
def add(num1,num2):
return num1+num2
new_addone = add
print(new_addone(2,3))
here in this example add function asign with new name called new_addone and new_addone variable act same as add function.
same case happened with calculation variable.
c1 variable is use to store a result.
con = input(f"do you want to contineus calculation with {c1} answer? type 'yes' or 'no' : ")
if con == 'no':
should_continue = True
else:
num1 = c1
from the statement we all know that what con variable do.
if con == no then program stop there execution otherwise first input (num1) is equal to result (c1) to perform futher operations.
Hope this post help you to learn something new thankyou
1 Comment
thanks bro