Simple Calculator Example Program using Python Programming Language
Learn to write a Simple Calculator Program using Python programming language.
Calculator program is probably the simplest program which a beginner can write and understand a lot of things about the language.
This Program takes two parameters ( two Numbers) from the user and it performs the Addition, Subtraction, Multiplication and Division on them.
Source Code for calculator.py
1 from functions import *
2
3 while True:
4 print("What do u want to do?")
5 print("1 Addition")
6 print("2 Subtraction")
7 print("3 Multiplication")
8 print("4 Division")
9 print("Enter Q or q to Exit")
10
11 choice = input("Enter Your Choice : ")
12
13 if choice == 'Q' or choice == 'q':
14 break
15
16 num1 = float(input("Enter Number 1 : "))
17 num2 = float(input("Enter Number 2 : "))
18
19 if choice == '1':
20 addition(num1,num2)
21 elif choice == '2':
22 subtraction(num1,num2)
23 elif choice == '3':
24 multiplication(num1,num2)
25 elif choice == '4':
26 division(num1,num2)
27 else:
28 print("Invalid Choice")
29
30 print("\n")
Source Code for functions.py
1 def addition(num1,num2):
2 result = num1 + num2
3 print("{0} + {1} = {2}".format(num1,num2,result))
4
5 def subtraction(num1,num2):
6 result = num1 - num2
7 print("{0} - {1} = {2}".format(num1,num2,result))
8
9 def multiplication(num1,num2):
10 result = num1 * num2
11 print("{0} * {1} = {2}".format(num1,num2,result))
12
13 def division(num1,num2):
14 if num2 == 0.0:
15 print("Cant Do Divide by Zero")
16 else:
17 result = num1 / num2
18 print("{0} / {1} = {2}".format(num1,num2,result))