Calculator

Tutorial for Python Functions : Building a Simple Calculator

In this tutorial, we’ll learn how to make good use of Python functions by building a simple calculator application. This calculator will perform basic operations like addition, subtraction, multiplication, and division.

Table of Contents

  1. Getting Started
  2. Addition Function
  3. Subtraction Function
  4. Multiplication Function
  5. Division Function
  6. User Interface
  7. Putting It All Together

1. Getting Started

Before diving into the functionality, let’s understand why we are using functions. Functions allow us to encapsulate a piece of code, making it reusable and easier to manage.

2. Addition Function

Let’s start by creating a function for the addition operation.

def add(a, b):
    return a + b

Example:

result = add(5, 3)
print(f"The sum is {result}.")

3. Subtraction Function

Now let’s create a function for the subtraction operation.

def subtract(a, b):
    return a - b

Example:

result = subtract(8, 3)
print(f"The difference is {result}.")

4. Multiplication Function

We’ll proceed with creating a function for multiplication.

def multiply(a, b):
    return a * b

Example:

result = multiply(5, 3)
print(f"The product is {result}.")

5. Division Function

Finally, let’s create a function for division, with a check for dividing by zero.

def divide(a, b):
    if b == 0:
        return "Cannot divide by zero."
    return a / b

Example:

result = divide(9, 3)
print(f"The quotient is {result}.")

6. User Interface

Now we’ll create a simple user interface to access these functions.

def calculator_interface():
    print("Simple Calculator")
    print("1: Addition")
    print("2: Subtraction")
    print("3: Multiplication")
    print("4: Division")
    choice = input("Select an operation (1/2/3/4): ")

    a = float(input("Enter the first number: "))
    b = float(input("Enter the second number: "))

    if choice == '1':
        print(f"The sum is {add(a, b)}.")
    elif choice == '2':
        print(f"The difference is {subtract(a, b)}.")
    elif choice == '3':
        print(f"The product is {multiply(a, b)}.")
    elif choice == '4':
        print(f"The quotient is {divide(a, b)}.")
    else:
        print("Invalid choice.")

Example:

calculator_interface()

7. Putting It All Together : Source Code

Now we’ll integrate all these functions to create the complete calculator application.

# Function for addition
def add(a, b):
    return a + b

# Function for subtraction
def subtract(a, b):
    return a - b

# Function for multiplication
def multiply(a, b):
    return a * b

# Function for division
def divide(a, b):
    if b == 0:
        return "Cannot divide by zero."
    return a / b

# User Interface
def calculator_interface():
    print("Simple Calculator")
    print("1: Addition")
    print("2: Subtraction")
    print("3: Multiplication")
    print("4: Division")
    choice = input("Select an operation (1/2/3/4): ")

    a = float(input("Enter the first number: "))
    b = float(input("Enter the second number: "))

    if choice == '1':
        print(f"The sum is {add(a, b)}.")
    elif choice == '2':
        print(f"The difference is {subtract(a, b)}.")
    elif choice == '3':
        print(f"The product is {multiply(a, b)}.")
    elif choice == '4':
        print(f"The quotient is {divide(a, b)}.")
    else:
        print("Invalid choice.")

# Run the interface
calculator_interface()

Output:

Simple Calculator
1: Addition
2: Subtraction
3: Multiplication
4: Division
Select an operation (1/2/3/4): 1
Enter the first number: 5
Enter the second number: 3
The sum is 8.0

Congratulations! You have built a simple calculator using Python functions. This tutorial demonstrates the importance of functions in breaking down your code into reusable, manageable pieces. You can expand this application by adding more features like handling more complex calculations or adding more operations.