Basic calculator program using Python
Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication or division depending upon the user input.
Approach :
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 numbers def multiply(num1, num2): return num1 * num2 # Function to divide two numbers def 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 = 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") |
chevron_right
filter_none
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
Recommended Posts:
- Program to create grade calculator in Python
- Python | Basic Program Crash Debugging
- Program for EMI Calculator
- C/C++ program to make a simple calculator
- Menu Driven C++ Program for a Simple Calculator
- Smart calculator in Python
- Python | Loan calculator using Tkinter
- How to make calculator using kivy | Python
- Python | Simple calculator using Tkinter
- Python | Simple GUI calculator using Tkinter
- Python | Distance-time GUI calculator using Tkinter
- Basic Operators in Python
- Basic Approximations in Python
- A basic Python Programming Challenge
- Python | Basic Gantt chart using Matplotlib
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : priyankakarande



