Write your first Python program: How to make a calculator program in Python?

What is Python?

Python basically is a widely used, general propose high level programming language which requires fewer lines of codes than C++ or Java.

If you are just learning Python,and  can understand its basics, and are interested in writing an easy and easily understandable program, this is the perfect link for you. Here, I’ll provide you with the code of one of your first Python programs, i.e. a calculator. It is pretty easy. You can just copy the following lines of codes and paste it in a text file and later change its extension to “.py”.

Code for calculator:

def main():
 o = input("what do you wanna do?(1 for + ,2 for - ,3 for *,4 for /): ")
 n1 = int(input("enter 1st num: "))
 n2 = int(input("enter 2nd num: "))
 if o != "1" and o != "2" and o != "3" and o != "4":
      print("invalid")
 elif o == "1":
      s = n1 + n2
 elif o == "2":
      s = n1 - n2
 elif o == "3":
      s = n1 * n2
 elif o == "4":
      s = n1 / n2
 print(s)
main()


Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: