×

Calculator Program in Python

Simple Calculator Program in Python

This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division.

Source Code

The following is the source code to perform simple calculations. In this program, we will perform addition, subtraction, multiplication, and division operations.

Function to perform addition operation
 def addition(num1, num2):                                    
     return num1 + number2                                    
 Function to perform subtraction operation
 def subtraction(num1, num2):                                
     return num1 - num2                                    
 Function to perform multiplication operation
 def multiplication(num1, num2):                                
     return num1 * num2                                    
 Function to perform division operation
 def division(num1, num2):                                    
     return num1 / num2                                    
 Choose operation
 print("Select desired operation: \n"                                
       "1.Add\n"                                            
       "2.Substraction\n"                                        
       "3.Multiplication\n"                                        
       "4.Division")                                            
while True:
# Take input from the user
operation = input("Choose operation(1/2/3/4): ")
# Check if operation is one of the four options if operation in ('1', '2', '3', '4'):    
number1 = float(input("Enter first number: "))     
number2 = float(input("Enter second number: "))   
  if operation == '1':     
#Print sum of two number        
 print(number1, "+", number2, "=", addition(number1, number2))     
elif operation == '2':     
# Print subtraction of two number         
print(number1, "-", number2, "=", subtraction(number1, number2))     
elif operation == '3':     
# Print multiplication of two number         
print(number1, "*", number2, "=", multiplication(number1, number2))     
elif operation == '4':     
# Print division of two number         
print(number1, "/", number2, "=", division(number1, number2))     
break else:     
print("Wrong operation selected")

Output

Here is the output:

CASE 1:

Calculator Program in Python

CASE 2:

Calculator Program in Python

Explanation:

Here, a user asks to enter two numbers. These numbers are stored in two different variables. Then select the desired option to perform operations. After this, the function will return the selected operation that represents different arithmetic operations. After taking input, we check the selected operation using if...elif...else statements. If the operation is valid, the value will be evaluated based on the respective operations, and finally, print the result. If we select any other input, it will print the wrong operation, and the loop continues until a valid option is selected.


Related Topics

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python program to perform the arithmetic operation

Python program to perform the arithmetic operation This program will write a code to perform some basic arithmetic operations like addition, subtraction, multiplication, exponent, modulus, and division. Here we first need...

2 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

Python Win32 Process

The Win32 process is essentially a Python procedure. This program provides access to advanced Win32 process creation and management features. Process objects are created through the Create method (the constructor)....

6 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Ternary operators in python

Starting from Python version 2.5, ternary operators are also known as Conditional operators. Using these operators, we can evaluate any problem based on a condition. It is an alternative and...

4 minutes read.

App Config Python

An XML file called App. Config serves as the document for any programme. In other terms, you can modify any configuration inside of it without going to edit the code...

7 minutes read.

Magento 2 Site optimization

Magento 2 Site optimization Magento 2 is a CMS, commonly referred to as Intensive efficiency. Improving the speed and reliability of your Magento 2 app helps clients to achieve a better user experience while...

2 minutes read.

Python Fit Transform

In machine learning, fit (), transform(), fit transform () methods are provided by scikit learn package. This package is used in model fitting and data processing. These methods are implemented...

2 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables. These are taken into account to do operations on values and variables. There are some special symbols in python...

4 minutes read.

Flutter with tensor flow in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

3 minutes read.