Basic calculator program using Python - GeeksforGeeks

19 Apr, 2020
Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input.Approach :
User choose the desired operation. Options 1, 2, 3 and 4 are valid.
Two numbers are taken and an if...elif...else branching is used to execute a particular section.
Using functions add(), subtract(), multiply() and divide() evaluate respective 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
# Python program for simple calculator  # Function to add two numbers def add(num1, num2):    return num1 + num2  # Function to subtract two numbers def subtract(num1, num2):    return num1 - num2  # Function to multiply two numbersdef multiply(num1, num2):    return num1 * num2  # Function to divide two numbersdef divide(num1, num2):    return num1 / num2  print("Please select operation -\n" \        "1. Add\n" \        "2. Subtract\n" \        "3. Multiply\n" \        "4. Divide\n")    # Take input from the user select = int(input("Select operations form 1, 2, 3, 4 :"))  number_1 = int(input("Enter first number: "))number_2 = int(input("Enter second 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("Invalid input")Output :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
My Personal Notes
arrow_drop_upSave
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

# Python program for simple calculator  # Function to add two numbers def add(num1, num2):    return num1 + num2  # Function to subtract two numbers def subtract(num1, num2):    return num1 - num2  # Function to multiply two numbersdef multiply(num1, num2):    return num1 * num2  # Function to divide two numbersdef divide(num1, num2):    return num1 / num2  print("Please select operation -\n" \        "1. Add\n" \        "2. Subtract\n" \        "3. Multiply\n" \        "4. Divide\n")    # Take input from the user select = int(input("Select operations form 1, 2, 3, 4 :"))  number_1 = int(input("Enter first number: "))number_2 = int(input("Enter second 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("Invalid input")
Output :
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

priyankakarande
vogelvikash1997
Python
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java