×

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 range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

How to Iterate through a Dictionary in Python

In this tutorial, we will learn how to interate throught the dictionary in the Python programming, using different approaches with example. Introduction to Dictionary in Python Dictionary is a special type of...

4 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Find Words in String Python

Python : Python programming language is considered a general purpose; it is a high-level programming language that is not much difficult but easier to learn. Python programming language is rich in...

5 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

Python Os sep

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

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 Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.

Python AIOHTTP

Python 3.5 introduced some new syntax that makes it simpler for developers to make asynchronous programmes and packages. Aiohttp, an HTTP client/server for asyncio, is one such package. In essence,...

3 minutes read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.