How To Make Basic Calculator Using Python Programming Langauge

In this lesson, you will learn about how to make basic calculator using Python Programming Language and believe me its very simple and fun. This calculator just performs basic arithmetic functions like addition, subtraction, multiplication, and division.  This simple calculator takes the input from the user and then performs the desired operation on the given input. All this is done in the following steps.

  • A user gives the input 2 times with their desire number like 1, 2, 3, and so forth.
  • Then if input evaluated/tested with if,…else statements to test the user given input and to decide what to do with this input.
  • Then we use functions for all the four operations mentioned earlier.
Recommended:  Advantages & Applications Of Python Programming Language

See the below example:

#Below code shows the steps for performing operations
# A form of basic calculator
Please select what you want to do 
1. Addition
2. Subtraction
3. Multiplication
4. Division
Please Select from above operations numbers 1, 2, 3, 4: 1
Please enter your first number: 10
Please enter the second number: 23
10 + 23 = 33

See the example code below:

# A simple program for making basic calculator
# This function use for addition 
def add(num1, num2): 
	return num1 + num2 
# This function use for subtraction
def subtract(num1, num2): 
	return num1 - num2 
# This function use for multiplication
def multiply(num1, num2): 
	return num1 * num2 
# This function use for division
def divide(num1, num2): 
	return num1 / num2 
print("Please select what you want to do  -n"  
		"1. Additionn"  
		"2. Subtractionn"  
		"3. Multiplicationn"  
		"4. Divisionn") 
# This step takes the input from the user 
select = input("Please choose your desired operation form 1, 2, 3, 4 :") 
number_1 = int(input("Please enter your first number: ")) 
number_2 = int(input("Please enter your first number: ")) 
if select == '1': 
	print(number_1, "+", number_2, "=", 
	add(number_1, number_2)) 
elif select == '2': 
	print(number_1, "-", number_2, "=", 
	subtract(number_1, number_2)) 
elif select == '3': 
	print(number_1, "*", number_2, "=", 
	multiply(number_1, number_2)) 
elif select == '4': 
	print(number_1, "/", number_2, "=", 
	divide(number_1, number_2)) 
else: 
	print("It is an Invalid input") 

Code Output:

#This is the output of code 
Please select what you want to do -
1. Addition
2. Subtraction
3. Multiplication
4. Division
Please Select from above operations numbers 1, 2, 3, 4: 1
Enter first number : 20
Enter second number : 10
20 + 10 = 30

So, this is all of how you can make a basic working calculator using the python programming language.

Jack Sparrow

Hay I am Admin Of This Website. I am very passionate about Computers and technology. This website is the result of my passion. I love to eliminate the barriers comes in the for seeking education. This website is the free source of learning about new technology and up to date knowledge.

Leave a Reply

Close Menu