Welcome to violet-cat-415996.hostingersite.com. In this tutorial, we are going to write a simple Calculator Program in Python and run it on different IDEs of python for creating a calculator we are using the mathematical operators, functions, conditional statements, and handling of user input to create our calculator.
In this tutorial, we are going to see the various python IDEs on which we are going to execute our calculator program.
What is an IDE?
An IDE i.e an Integrated Development Environment refers to a software application that provides extensive software development abilities to computer programmers. The IDEs increase the programmer’s productivity with the help of python edition features of source code, building the executables, and debugging. Most of the modern IDEs we come across have intelligent code completion features. Hence the IDE helps computer programmers to understand the various aspects of writing a computer program.
Calculator Program in Python on Different IDEs
So Let’s begin with our calculator program
Calculator Program using cmd
#Progran to perform addition ,substraction,Multiplication,Division
from sys import argv
print("The number of command line agruments :",len(argv))
sum=0
sub=0
mul=1
div=1
for i in range(1,len(argv)):
print(argv[i])
sum = sum + int(argv[i])
print("The addition is", sum)
for i in range(1,len(argv)):
sub = int(argv[i])-sub
print("The subtraction is",(~sub+1))
for i in range(1,len(argv)):
mul = int(argv[i]) * mul
print("The multiplication is", mul)
for i in range(1,len(argv)):
div = div / int(argv[i])
print("The division is", div)
Explanation:
- Here we are going to execute our program by taking command line arguments. We are importing the argv from the system module where the argv is an array for the command line arguments in python.
- In the command line argument the first element of the array of sys.argv() is the name of the program itself.
- In this program, we are performing 4 different operations addition, subtraction, multiplication, and division for we are taking four different variables add, sub, mul, and div for storing the result.
- The len(argv) counts the total length of arguments.
Output
Calculator Program in Python using Pycharm IDE
#Functions for addition,substraction ,multiplication,division
def add(x, y):
return x + y
# It subtracts two numbers
def subtract(x, y):
return x - y
# It multiplies two numbers
def multiply(x, y):
return x * y
# It divides two numbers
def divide(x, y):
return x / y
print("Select To calculate.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
# take input from the user
ch = input("Enter the choice: ")
# choose from the four options
if ch in ('1', '2', '3', '4'):
no1 = float(input("Enter Number1: "))
no2 = float(input("Enter Number2: "))
if ch == '1':
print(no1, "+", no2, "=", add(no1, no2))
elif ch == '2':
print(no1, "-", no2, "=", subtract(no1, no2))
elif ch == '3':
print(no1, "*", no2, "=", multiply(no1, no2))
elif ch == '4':
print(no1, "/", no2, "=", divide(no1, no2))
# check if user wants another calculation
# break the while loop if answer is no
calc= input("Continue Calculation?(yes/no): ")
if calc== "no":
break
else:
print("Invalid Input")
Explanation:
- In this program, we have defined four functions addition, subtraction, multiplication, and division.
- We have provided the choices for add, subtract, multiply and divide. The user has to select from these 4 choices
- Calc is the variable that stores the value of whether the user wants to continue the calculation then he will type “yes” else type “no” to break the loop.
- We are going to execute the program using the pycharm IDE. This IDE is the most widely used IDE created by JetBrains. Its community edition and professional edition are available in the market and it helps in the development of large development projects.
- Steps to run the program in pycharm IDE Right click on Project->New->Python File->”Filename.py”.
- Write the code->Click on ”Run” to execute the program
Output
Calculator Program using IDLE(Python)
IDLE (Integrated Development and Learning Environment) is a default editor that accompanies Python. This IDE can be used on Windows, Linux, and Mac OS. The IDLE is suitable for all beginners started learning python. Steps to run the above calculator program in Python using IDLE(Python) are:
- Open IDLE Shell 3.10.2
- Click on File->New File->Save->Save as Type “Python Files”
- Click on Run->Run Module
Output
Hence, after running on IDLE we will get the above output.
Calculator Program using Visual Studio Code
The visual studio code developed by Microsoft is a very lightweight and open-source IDE, mostly used in the development of python programs having good code debugging and editing features.
It provides all those strong features that most paid IDEs offer.
Steps to run the above calculator program in Python using Visual Studio Code.
- Install Visual Studio Code(Link on how to install visual studio code)
- Click on File->New Text File->Select a language of python to open the editor->Write the code
- Click on File->Save->Save as type->Python Files->Click on a Run command to see the execution
- The following output is visible once you click on the Run command on the Visual Studio Code.
Output
Calculator program using Thonny
Thonny is an IDE for python that is designed for all python beginners. It provides various ways to step through the code and expression evaluation.
It is an ideal platform for learning python programming as provides a simple debugger, supports automatic syntax detection, and provides a detailed view of the variables used in python.
Steps to Run calculator program in python.
- Intall thonny(link for installing thonny).
- Click on File->New File->Type the code->Save the file as type python files with Filename.py.
- Install the package pip3 install thonny.
- Click the command Run, you will see the following output.
Output
Calculator program using PyDev
PyDev is one of the most preferred open-source IDE by the Developers, It is flexible, and distributed as a third-party plugin for the Eclipse IDE. It is a strong python interpreter providing good support to python web development, debugging, and code analysis. It is also having the feature of auto code completion and Django integration.
Steps to Run the Calculator Program in Python on PyDev:
- Install Eclipse IDE and PyDev (Complete guide on how to install Eclipse IDE and PyDev)
- Click on File->New PyDev Project->Write the Project Name
- Click on File->Python Module->Select your Project Name as the Source Folder->Create a package->Create the Python file->Write the code.
- After clicking on Run Command you will get the following output for the calculator program.
Output
Calculator Program using Jupyter
Jupyter is widely used for machine learning and Data Science. It is easy to use, supports code sharing, and numerical calculations, and allows code sharing and visualization.
It helps in combining the code, images, and text.
Jupyter notebook can easily be launched using anaconda distribution.
Steps to run the calculator program in Python using jupyter.
- Intall anaconda here ( complete guide to install Jupyter notebook)
- Open Anaconda Navigator ->Select Jupyter Launch ->
- Type the python code->Click on Run Command to see the following output.
Output
Conclusion
There are a number of python IDEs available in the market having various features, you can choose which one to pick depending on the requirements. Here we have some of the python IDEs in which you can execute the calculator program using python. Hence different IDEs can be used in different environments.
Hope you find this article helpful. For more articles on python keep visiting our website.
Also Read:
- What is web development for beginners?
- Python | Check Armstrong Number using for loop
- Python | Factorial of a number using for loop
- What does if __name__ == __main__ do in Python?
- Python | CRUD operations in MongoDB
- Create your own ChatGPT with Python
- Radha Krishna using Python Turtle
- Python Programming Examples | Fundamental Programs in Python
- Python | Delete object of a class
- Python | Modify properties of objects
- Python classmethod
- Python | Create a class that takes 2 parameters, print both parameters
- Python | Create a class, create an object of the class, access, and print property value
- Python | Find the maximum element in a list using lambda and reduce()
- Python | filter numbers(that are not divisible by 3 and 4) from a list using lambda and filter()
- Python | lambda function that returns the maximum of 2 numbers
- Python | Convert all strings of a list to uppercase using lambda
- Python | Square numbers of a list using lambda
- Python | Reverse and convert a string to uppercase using lambda
- Python | Convert String to uppercase using lambda
- Python | Reverse a list using lambda
- Python | Calculator using lambda
- Python | Square of a number using lambda
- Python | Multiply numbers using lambda
- Python | lambda with None
- Python | lambda with pass statement
- Python | Add numbers using lambda
- Python | Print Namaste using lambda
- Iterate over a string in Python
- Python | join() | Join list of strings