We all know about calculators and what it does. This blog is about creating a basic calculator using Python. If you are a beginner, this might help you.

This calculator will have only four basic arithmetic operations. Those are addition, subtraction, multiplication, and division. We are all familiar with these.

This calculator concept is simple. We will take input from the user and print the result. It will be mainly a function (`calculator`

), which will take three parameters. The first parameter will be the operation type. And the last two will be the number.

So, we have to define a function where we have three arguments. And then, we will check the type to print the result based on the operation type. Let’s see the code to get a better understanding.

def calculator(operation, num1, num2): if operation == '+': res = num1 + num2 print(res) elif operation == '-': res = num1 - num2 print(res) elif operation == '*': res = num1 * num2 print(res) elif operation == '/': res = num1 / num2 print(res) else: print('This calculator does not support this operation :)') calculator('+', 2, 2) # 4 calculator('-', 10, 5) # 5 calculator('*', 10, 10) # 100 calculator('/', 100, 10) # 10 calculator('', 100, 10) # This calculator does not support this operation :)

I hope you got the idea. But this is not the end. This one is undoubtedly a rudimentary type of calculator. You can add more features to it. For example, you can take user input from the command line. And you can validate user input if they provide invalid inputs. There are a lot’s features that you can add to this.

So, there can be many more. And it will help you a lot when you put in extra features by yourself.

And finally, best wishes to you.