# A Command-line Calculator using Python 3

Kushal introduced us to command-line parsing module argparse in Python3 on Wednesday in dgplug. Well now I am having the real fun with Python.

I have made a simple calculator using argparse that calculates addition, subtraction, division and multiplication. Run the code in Linux terminal as given below in the image.

The code:

```import argparse

val = a+b
return val

#Subtraction function
def sub(a,b):
val = a-b
return val

#Division function
def div(a,b):
val = a/b
return val

#Multiplication function
def multi(a,b):
val = a*b
return val

#Main function
def Main():
parser = argparse.ArgumentParser()

args = parser.parse_args()

#Optional arguments
elif args.fsub:
print("The subtraction result of {} and {} is {}".format(args.num1,args.num2,(sub(args.num1,args.num2))))
elif args.fdiv:
print("The division result of {} and {} is {}".format(args.num1,args.num2,(div(args.num1,args.num2))))
elif args.fmulti:
print("The multiplication result of {} and {} is {}".format(args.num1,args.num2,(multi(args.num1,args.num2))))
else:
print("Error:Requires an argument to perform an action")

if __name__ == '__main__':
Main()

```

This works like : Leave comments for modification of the code 🙂

## 9 thoughts on “A Command-line Calculator using Python 3”

1. Calci | Trishna Guha says:

[…] used Python3 to build this command line tool. You will find that I had already made a blog post on Command line calculator earlier. Now this new post comes with some edits of the earlier one and another important thing is […]

Like

2. dsilentchaos says:

I have never use python language. Done d basics in c…Should I start python too? Or continue wd c ? Thanks.

Like

3. dsilentchaos says:

I have never used python language. Done d basics in c…Should I start python too? Or continue wd c ? Thanks.

Liked by 1 person

• Trishna Guha says:

It is good to hear that you have done the basics of C, because if you know C well it becomes easier to learn Python or any other programming language. The syntax of python programming language is really simple. C is a procedural programming language and Python is object oriented programming language. So before starting with object oriented programming one should learn procedural programming. Since you have done the basics of C, so I don’t think you’ll face problem understanding object oriented programming 🙂 .If you are really interested, you can start learning Python. You can start learning Python here http://www.codecademy.com and use Python documentation as reference.
Feel free to ask if you have anymore query 🙂

Liked by 1 person

• dsilentchaos says:

Thanks.Wll check d site then. 🙂

Liked by 1 person

4. dsilentchaos says:

For python only? Okay. I just need smthing where I can have a step by step tutorial..like a classroom teaching.
Anyways,
Thanks 🙂

Like

5. Anton says:

So the point is that if only one parameter is needed to be choosen then grouping of arguments should be used. Otherwise simple adding arguments to a parser will work. That for example allows to calculate with different operators at the same time: ‘python3 calc.py 3 5 -fa -fs’ will work.

Like