Write a simple calculator program in Python 3

Simple Calculator Program in Python 3 :

In this tutorial, we will learn how to create a Calculator using python 3. We are going to create only a simple calculator that can perform addition, subtraction, multiplication and division . You can add any other mathematical operations to it if you want.
Before going through the code, let me show you how it is going to work :

Algorithm we are using in this program :

1. We are using one infinite loop to get input from user continuously. That means, user can use the calculator as many time as he wants.
2. Calculator will exit if user will enter ‘q’ as input
3. For each calculation (add, subtract, multiply, divide) we have different functions.
4. Using ‘if-else’ condition, we are checking what type of calculation user wants to do.

Program :

print ("Welcome !!")

#function to add two numbers
def add(first_num,second_num):
	print ("{} + {} = {}".format(first_num,second_num,first_num + second_num))

#function to substract one number from another
def sub(first_num,second_num):
	print ("{} - {} = {}".format(first_num,second_num,first_num - second_num))

#function to multiply two numbers
def mul(first_num,second_num):
	print ("{} * {} = {}".format(first_num,second_num,first_num * second_num))

#function for division
def div(first_num,second_num):
	print ("{}/{} = {}".format(first_num,second_num,first_num/second_num))

print ('''
Enter + for addition
Enter - for Substraction
Enter * for multiplication
Enter / for Division
Enter q to Quit the program
''')

while True :
	print("")
	user_input = input ("Enter calculator type : ")
	if user_input == '+' :
		print ("To calculate (number 1 + number 2) :")
		add(int(input("Enter number 1 : ")),int(input("Enter number 2 : ")))
	elif user_input == '-':
		print ("To calculate (number 1 - number 2) :")
		sub(int(input("Enter number 1 : ")), int(input("Enter number 2 : ")))
	elif user_input == '*':
		print ("To calculate (number 1 * number 2) :")
		mul(int(input("Enter number 1 : ")), int(input("Enter number 2 : ")))
	elif user_input == '/':
		print ("To calculate (number 1 / number 2) :")
		div(int(input("Enter number 1 : ")), int(input("Enter number 2 : ")))
	elif user_input == 'q':
		break
		print ("")
	else:
		print ("Please Enter a Valid Input !!")

Sample Output :

Welcome !!

Enter + for addition
Enter - for Substraction
Enter * for multiplication
Enter / for Division
Enter q to Quit the program


Enter calculator type : +
To calculate (number 1 + number 2) :
Enter number 1 : 12
Enter number 2 : 14
12 + 14 = 26

Enter calculator type : -
To calculate (number 1 - number 2) :
Enter number 1 : 44
Enter number 2 : 9
44 - 9 = 35

Enter calculator type : *
To calculate (number 1 * number 2) :
Enter number 1 : 123
Enter number 2 : 4
123 * 4 = 492

Enter calculator type : /
To calculate (number 1 / number 2) :
Enter number 1 : 123
Enter number 2 : 3
123/3 = 41.0

Enter calculator type : 1
Please Enter a Valid Input !!

Add more Actions to this calculator :

You can add more actions to this calculator program. Just add more functions for each type of calculations and add more if-elif conditions. Happy Coding 🙂

Sitewide-USD 336x280

Leave a Reply