{"id":208,"date":"2018-01-01T06:17:00","date_gmt":"2018-01-01T05:17:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/01\/01\/how-to-create-a-simple-calculator-in-python-try-it\/"},"modified":"2020-08-22T11:05:59","modified_gmt":"2020-08-22T09:05:59","slug":"how-to-create-a-simple-calculator-in-python-try-it","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-a-simple-calculator-in-python-try-it\/","title":{"rendered":"How to Create a Simple Calculator in Python (Try it!)"},"content":{"rendered":"<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\">In this lesson I would teach you how to create a simple calculator in the Python Programming Language. Python 3.6 is used in this tutorial, but the program would also work with other versions of python.<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/4.bp.blogspot.com\/-f5E3JOsy4M8\/WknSV0GEkhI\/AAAAAAAAAlI\/kVB4AJXHWM4NVDzk49dZac3B7jvyuyIwACLcBGAs\/s1600\/How-To-Create-a-Simple-Calculator-in-Python.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" loading=\"lazy\" border=\"0\" data-original-height=\"736\" data-original-width=\"1350\" height=\"217\" src=\"https:\/\/4.bp.blogspot.com\/-f5E3JOsy4M8\/WknSV0GEkhI\/AAAAAAAAAlI\/kVB4AJXHWM4NVDzk49dZac3B7jvyuyIwACLcBGAs\/s400\/How-To-Create-a-Simple-Calculator-in-Python.jpg\" width=\"400\" \/><\/a><\/div>\n<p><b>A Brief Explanation<\/b><br \/>The program contains four functions: Add, Subtract, Multiply and Divide. Each of the function takes two parameters, a and b which represents the two numbers to be evaluated.<br \/>The program begins by displaying a menu of four items, with numbers representing each item<br \/>1 for Add<br \/>2 for Subract<br \/>3 for Multiply<br \/>4 for Divide<\/p>\n<p>User is prompted to choose from the four operations. The user choice is stored in a variable called operation. Next the user is prompted to enter the first number and the second number.<br \/>Based on the user&#8217;s choice of operation, the appropriate function is called.<br \/>The result of the operation is displayed&nbsp; on the output using the print function.<\/p>\n<p>You can find the code below. Feel free to copy and use<\/p>\n<\/div>\n<\/div>\n<p>  <ins      style=\"display:block; text-align:center;\"      data-ad-layout=\"in-article\"      data-ad-format=\"fluid\"      data-ad-client=\"ca-pub-7041870931346451\"      data-ad-slot=\"8227894917\"><\/ins> <\/p>\n<pre style=\"background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &quot;arial&quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;\"><code style=\"color: black; word-wrap: normal;\"> import sys  <br \/> #****************************************************************************************  <br \/> #PROGRAM BY:  KINDSON THE GENIUS                           *                         <br \/> #SECTION:    PYTHON TUTORIALS                               *  <br \/> #DATE:     1ST JANUARY 2018                                   *  <br \/> #QUESTION No.  YOUR FIRST PROGRAM IN 2018                    *  <br \/> #****************************************************************************************  <br \/>   <br \/> print(\"n*** PROGRAM TO CREATE SIMPLE MENU-BASED CALCULATOR ******nn\")  <br \/>   <br \/>   <br \/> # THE add FUNCTION TO PERFORM ADDITION OF TWO NUMBERS  <br \/> def add(a, b):  <br \/>   return a + b  <br \/>   <br \/>   <br \/> # THE subtract FUNCTION TO PERFORM SUBTRACTION FOR TWO NUMBERS  <br \/> def subtract(a, b):  <br \/>   return a - b  <br \/>   <br \/>   <br \/> # THE multiply FUNCTION TO PERFORM MULTIPLICATION OF TWO NUMBERS  <br \/> def multiply(a, b):  <br \/>   return a * b  <br \/>   <br \/> # THE DIVIDE FUNCTION TO PEFORM DIVISION FOR TWO NUMBERS  <br \/> def divide(a, b):  <br \/>   return a \/ b  <br \/>   <br \/> #DISPLAY THE MENU  <br \/> print(\"CHOOSE AN OPERATION. \")  <br \/> print(\"1. ADD\")  <br \/> print(\"2. SUBTRACT\")  <br \/> print(\"3. MULTIPLY\")  <br \/> print(\"4. DIVIDE\")  <br \/> print(\"5. EXIT\")  <br \/>   <br \/> #GET USER INPUT  <br \/> operator = input(\"Select an operation (1,2,3,4,5): \")  <br \/>   <br \/> #EXIT THE PROGRAM IF THE INPUT IS 5  <br \/> if operator == '5':  <br \/>   sys.exit()  <br \/>   <br \/> number1 = int(input(\"Enter first number:\"))  <br \/> number2 = int(input(\"Enter second number:\"))  <br \/>   <br \/> #CONVERT THE INPUTS TO INTEGER  <br \/> number1 = int(number1)  <br \/> number2 = int(number2)  <br \/>   <br \/> if operator == '1':  <br \/>   result = add(number1,number2)  <br \/>   print(number1,\"+\",number2,\"=\", result)  <br \/>   <br \/> elif operator == '2':  <br \/>   result = subtract(number1,number2)  <br \/>   print(number1,\"-\",number2,\"=\", result)  <br \/>   <br \/> elif operator == '3':  <br \/>   result = multiply(number1,number2)  <br \/>   print(number1,\"*\",number2,\"=\", result)  <br \/>   <br \/> elif operator == '4':  <br \/>   result = add(number1,number2)  <br \/>   print(number1,\"\/\",number2,\"=\", result)  <br \/>     <br \/> else:  <br \/>   print(\"Invalid input\")  <br \/>   <br \/>   <br \/>   <br \/><\/code><\/pre>\n<div style=\"color: #555555; font-size: 18px; line-height: 30px; text-align: justify;\">\n<div style=\"font-family: 'segoe ui';\"><b>How to Run&nbsp;<\/b><\/p>\n<ul>\n<li>Open Python Idle IDE<\/li>\n<li>Click on File&gt; New File<\/li>\n<li>Copy and paste the program<\/li>\n<li>Save the file with a name<\/li>\n<li>Click on Run &gt; Run Module<\/li>\n<\/ul>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson I would teach you how to create a simple calculator in the Python Programming Language. Python 3.6 is used in this tutorial, &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/208"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":1469,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/208\/revisions\/1469"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}