Saturday, November 16, 2024
Google search engine
HomeLanguagesPython Program to Make Basic Calculator

Python Program to Make Basic Calculator

Simple calculator program in python; In this tutorial, you will learn how to create a basic calculator that can add, subtract, multiply or divide depending upon the input from the user.

To understand this python program, you should have knowledge of python functions.

To create a basic method in python program, which is used to perform arithmetic operations.

1. Addition

Let’s do our first operation of Addition here:

def add(x, y):
   return x + y;

We used a simple function so that our code remains modular.

2. Subtraction

Second operation involves subtracting two numbers:

def subtract(x, y):
   return x - y;

3. Multiplication

Third operation involves multiplying two numbers:

def multiply(x, y):
   return x * y;

4. Division

Fourth operation involves dividing two numbers:

def devide(x, y):
   return x / y;

Calculator Program in Python using Functions

# Program create a simple calculator

# This function adds two numbers 
def add(x, y):
   return x + y

# This function subtracts two numbers 
def subtract(x, y):
   return x - y

# This function multiplies two numbers
def multiply(x, y):
   return x * y

# This function divides two numbers
def divide(x, y):
   return x / y

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user 
choice = input("Enter choice(1/2/3/4): ")

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")

Output

Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4):  1
Enter first number:  10
Enter second number:  15
10.0 + 15.0 = 25.0

Recommended Python Programs

  1. Python Program to Add Two Numbers
  2. Python Program to Find/Calculate Sum of n Numbers
  3. Python Program to Find/Calculate Average of 3, 4, 5…n numbers
  4. Python Program to Print ASCII Value of Character
  5. Write a Program to Calculate Simple Interest in Python
  6. Python Program to Compute Compound Interest
  7. Leap Year Program in Python
  8. Python Program to Print Star Pattern
  9. Number Pattern Programs in Python
  10. Python Program to Print Even and Odd numbers From 1 to N
  11. Python Abs() Function: For Absolute Value
  12. How to Check Whether a Number is Fibonacci or Not in Python
  13. Python: Program to Find Power of Number
  14. Python Program to Reverse a Numbers
  15. Python Program to Find Smallest/Minimum of n Numbers
  16. Python Program to Find Largest/Maximum of n Numbers
  17. Python Program to Find The Net Bill Amount After Discount
  18. Python Program to Print Numbers From N to 1 and 1 to N
  19. Python Program to Print Numbers Divisible by 3, 5, 7
  20. Python Program to Print Prime Number 1 to N
  21. How to Find Square of Number in Python
  22. Python Program to Calculate Cube of Number
  23. Python Program to Find LCM of Two Numbers
  24. BMI (Body Mass Index) Calculator in Python
  25. Palindrome Program in Python using while loop, Function, etc
  26. Python: Program to Count Total Number of Bits in Number
  27. Python Random Number Generator Code
  28. Python Program to Calculate n-th term of a Fibonacci Series
  29. Zip Zap Zoom Python Program
  30. Python: program to convert Celsius to Fahrenheit
  31. Python Program to Swap Two Numbers
  32. Python Program to Get Standard Deviation
  33. Python Program to Find the Variance
  34. Python Program to Convert Height in cm to Feet and Inches
  35. Python Program to Convert Meters into Yards, Yards into Meters
  36. Python Program to Convert Kilometers to Meters, Miles
  37. Python Program to Find Perfect Number
  38. Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
  39. Python: Program to Convert Decimal to Binary, Octal and Hexadecimal
  40. Python Program to Find Roots of Quadratic Equation
  41. Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
  42. Python Program to Check Given Input is Alphabet, Number or Special Character
  43. Python Program to Check IF a Number is Power of Another Number
  44. Python Check Binary Representation of Given Number is Palindrome or Not
  45. Python Program to Draw a Pie Chart
  46. Python Program Input the Radius of Circle and Compute the Area
  47. Python Program to Calculate the Area of a Rectangle
  48. Python Program to Calculate Area of Triangle
  49. Python Program to Find Area and Circumference of Circle using Radius
  50. Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
  51. Python Program to Print Binary Value of Numbers From 1 to N
RELATED ARTICLES

Most Popular

Recent Comments