Create a Calculator using Python without Importing Modules

Once upon a time, my father was after my life to learn tables. Every second-third day I used to get scolded upon this. But one bright day I made a calculator using python myself and presented it to him. After that vivid day, he never asked about tables. Yess!

Hey guys, welcome to Technifyed I’m Vasav Trehan and today I’ll teach you how to create your own Basic Calculator using Python. Surprising isn’t it!

The calculator which we’ll be creating today will be able to perform addition, subtraction, multiplication & division for 2 numbers/digits.

To continue further you will need to know Functions, Variables, Numbers & Conditional statements. Well without waiting even for a second let’s start.

Here we go with the first line of code 

def calc(num1, opr, num2):

We know that “def” is the initial word in a function followed by the name in this case “calc” there are 3 variables first num1 second or which is operation and third num2.

if opr == '+':

       return num1 + num2

This is a conditional statement & here we tell Python that if the operation is ‘+’ then add the two numbers. You can replace return with print but then you need to put the calculation in between brackets. We do the same for subtraction, multiplication & division like this.

if opr == '-':

        return num1 - num2

if opr == '*':

        return num1 * num2

if opr == '/':

        return num1 / num2

This piece of code is optional to put

else:

    print('This calculator performs only the 4 basic operations')

    

In this piece of code, we’re telling Python says “This calculator performs only the 4 basic operations” if the user inputs an operation or something else that this calculator can’t perform.

See also  How to Get Better in Coding?

This is the code in all-

def calc(num1, opr, num2):

    if opr == '+':

        return num1 + num2

    if opr == '-':

        return num1 - num2

    if opr == '*':

        return num1 * num2

    if opr == '/':

        return num1 / num2

    else:

        print('This calculator performs only the 4 basic operations')

Input

calc(3, '+', 6)

Output

9

You May Also Like

Calculate Area of Figures using Python

Latest

test

Voyager Scholarship: Voyager Scholarship 2025 Application, Enrollment & Tips

The Voyager Scholarship is an esteemed program that provides...

Spokeo Artificial Intelligence Scholarship 2024 Application, Enrollment & Tips

Artificial Intelligence (AI) has become an integral part of...

Pizza Hut Scholarship: Pizza Hut Scholarship 2024 Application, Enrollment & Tips

The Pizza Hut Scholarship Program is a post-high school...

Newsletter

Don't miss

test

Voyager Scholarship: Voyager Scholarship 2025 Application, Enrollment & Tips

The Voyager Scholarship is an esteemed program that provides...

Spokeo Artificial Intelligence Scholarship 2024 Application, Enrollment & Tips

Artificial Intelligence (AI) has become an integral part of...

Pizza Hut Scholarship: Pizza Hut Scholarship 2024 Application, Enrollment & Tips

The Pizza Hut Scholarship Program is a post-high school...
Vasav Trehan
Vasav Trehanhttps://technifyed.com
Vasav is the brain behind Technifyed.com. His love for Technology and Programming led him to kick-start this flabbergasting website and his Instagram account @technifyedofficial. His motive is to Simply Tech for Everyone.

test

Voyager Scholarship: Voyager Scholarship 2025 Application, Enrollment & Tips

The Voyager Scholarship is an esteemed program that provides financial aid, opportunities, and support to students aspiring to pursue a career in public service....

Spokeo Artificial Intelligence Scholarship 2024 Application, Enrollment & Tips

Artificial Intelligence (AI) has become an integral part of our modern world and is transforming various industries. As a result, several organizations are providing...