×

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

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Python Loop through a Dictionary

Introduction in this tutorial, we will discuss in python How to Loop Through a Dictionary. In contrast to other Data Types, which can only retain a single value as an element, a Dictionary...

4 minutes read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Cx_Oracle Python with Example

Python Programming Language: 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...

6 minutes read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Get index of element in array in python

Index : Index is a number where the element is located in the given list. In other words it is the position of the element in the list. Index can also...

4 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.