Create a Simple Calculator using Python

Simple Calculator using Python Functions

In this tutorial, we will learn how to create a simple calculator using python functions to perform the arithmetic operations like Addition, Subtraction, Multiplication, and Division by getting the inputs from the user.

Python is a language that is easy to learn and use. Python is a great language for beginners and experienced programmers alike. Python is an interpreted, high-level, general-purpose programming language.

See also  How to Install Python 3.11 on Ubuntu / Linux Mint
See also  Real-Time Currency Converter in Python – Fast and Easy Method

Simple Calculator using Python using Functions

First, we will create the functions to add, sub, mul, and div two numbers. The function will take two parameters, a, and b, and return the respective arithmetic operations of the two numbers.

# Create a Simple Calculator using Python

#Functions defined for arithmetic Operations

def sum(a,b):
    return a + b
def sub(a,b):
    return a - b
def mul(a,b):
    return a * b
def div(a,b):
    return a / b

# Requesting for User inputs

x = float(input("Enter the first number: "))
y = float(input("Enter the Second number: "))
operations = input("Opertion (sum / sub / mul / div) : ")

# if else Loops to Perform opertions

if operations == 'sum':
    print (sum (x,y))
elif operations == 'sub':
    print (sub(x,y))
elif operations == 'mul':
    print (mul(x,y))
elif operations == 'div':
    print (div(x,y))
else:
    print ("Invalid input")

The output of Simple Calculator

PS E:\Users\adminpc> py .\basic1.py
Enter the first number: 1
Enter the Second number: 1
Opertion (sum / sub / mul / div) : sum
2.0

PS E:\Users\adminpc> py .\basic1.py
Enter the first number: 1024
Enter the Second number: 2
Opertion (sum / sub / mul / div) : mul
2048.0

Conclusion

From this tutorial, you have learned how to create a simple calculator using python functions

Do let us know your comments and feedback in the comments section below.

If my articles on TipsonUNIX have helped you, kindly consider buying me a coffee as a token of appreciation.

Buymeacoffee

Thank You for your support!!

Spread the love

Leave a Reply