TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python make simple Calculator

Python make simple Calculator for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python Program to Make a Simple Calculator

In Python, we can create a simple calculator for performing the different arithmetical operations, such as addition, subtraction, multiplication, and division.

Approach:

  • We can choose the desired operation from the option of a, b, c, and d.
  • We can take two numbers, and if… elif… else, branching is used for executing the particular operation.
  • We will use add(), subtract(), multiply() and divide() function for evaluation the respective operation in the calculator.

Example:

Please select operation -
a. Add
b. Subtract
c. Multiply
d. Divide
Select operations form a, b, c, d: "c"  
Please enter first number: 11
Please enter second number: 4
11 * 4 = 44

Code: for Simple Calculator

def add(P, Q):  
   # This function is used for adding two numbers 
   return P + Q 
def subtract(P, Q): 
   # This function is used for subtracting two numbers 
   return P - Q 
def multiply(P, Q): 
   # This function is used for multiplying two numbers 
   return P * Q 
def divide(P, Q): 
   # This function is used for dividing two numbers  
   return P / Q  
# Now we will take inputs from the user  
print ("Please select the operation.")  
print ("a. Add")  
print ("b. Subtract")  
print ("c. Multiply")  
print ("d. Divide")  
  
choice = input("Please enter choice (a/ b/ c/ d): ")  
  
num_1 = int (input ("Please enter the first number: "))  
num_2 = int (input ("Please enter the second number: "))  
  
if choice == 'a':  
   print (num_1, " + ", num_2, " = ", add(num_1, num_2))  
  
elif choice == 'b':  
   print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))  
  
elif choice == 'c':  
   print (num1, " * ", num2, " = ", multiply(num1, num2))  
elif choice == 'd':  
   print (num_1, " / ", num_2, " = ", divide(num_1, num_2))  
else:  
   print ("This is an invalid input")  

Output:

Case - (1):

Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d):  d
Please enter the first number:  1
Please enter the second number:  2
1 / 2 = 0.5

Case - (2):

Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): b
Please enter the first number:  12
Please enter the second number:  11
12 - 11 = 1





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf