Change language

Basic calculator program using Python

|

Create a simple calculator that can perform basic arithmetic operations such as addition, subtraction, multiplication, or division based on user input. 
Match:

  • The user selects the desired operation. Choices 1, 2, 3 and 4 are valid.
  • Using two numbers and branching if… elif… else is used to execute a specific section.
  • Using the add (), subtract () functions, multiply () and split () evaluate the corresponding operations.
  • Example:

     Please select operation - 1. Add 2. Subtract 3. Multiply 4. Divide Select operations form 1, 2, 3, 4: 1 Enter first number: 20 Enter second number: 13 20 + 13 = 33 

    Exit:

     Please select operation - 1. Add 2. Subtract 3. Multiply 4. Divide Select operations form 1, 2, 3 , 4: 1 Enter first number: 15 Enter second number: 14 15 + 14 = 29 

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers


    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method

    # Python program for a simple calculator

     
    # Function for adding two numbers

    def add (num1, num2):

    return num1 + num2 

     
    # Function to subtract two numbers

    def subtract (num1, num2):

      return num1 - num2

      
    # Function for multiplying two numbers

    def multiply (num1, num2):

    return num1 * num2

     
    # Function for dividing two numbers

    def divide (num1, num2):

      return num1 / num2

     

    print ( "Please select operation -"

    "1. Add "

      " 2. Subtract "

      " 3. Multiply "

      " 4. Divide " )

      

     
    # Accept user input

    select = input ( "Select operations form 1, 2, 3, 4: " )

      

    number_1 = int ( input ( "Enter first number:" ))

    number_2 = int ( input ( "Enter second num ber: " ))

      

    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 ( "Invalid input" )