Simple Calculator Program in Python
This Python program implements a basic calculator with operations for addition, subtraction, multiplication, and division.
Python Code:
"""
A simple calculator program in Python.
"""
def add(num1, num2):
"""
Function to add two numbers.
:param num1: The first number.
:param num2: The second number.
:return: The sum of num1 and num2.
"""
return num1 + num2
def subtract(num1, num2):
"""
Function to subtract two numbers.
:param num1: The first number.
:param num2: The second number.
:return: The difference of num1 and num2.
"""
return num1 - num2
def multiply(num1, num2):
"""
Function to multiply two numbers.
:param num1: The first number.
:param num2: The second number.
:return: The product of num1 and num2.
"""
return num1 * num2
def divide(num1, num2):
"""
Function to divide two numbers.
:param num1: The dividend.
:param num2: The divisor (must not be zero).
:return: The result of dividing num1 by num2.
:raises ZeroDivisionError: If num2 is zero.
"""
if num2 == 0:
raise ZeroDivisionError("Cannot divide by zero")
return num1 / num2
# Example usage
num1 = 10.5
num2 = 5.2
# Perform operations
sum_result = add(num1, num2)
difference = subtract(num1, num2)
product = multiply(num1, num2)
quotient = divide(num1, num2)
# Output results
print(f"Sum: {sum_result}")
print(f"Difference: {difference}")
print(f"Product: {product}")
print(f"Quotient: {quotient}")
Explanation:
The Python code above defines a simple calculator program with the following components:
- Function Definitions: Functions (
add
,subtract
,multiply
,divide
) are defined to perform basic arithmetic operations. - Function Documentation: Each function includes docstring comments to describe their purpose, parameters, return values, and exceptions raised.
- Exception Handling: The
divide
function handles the case of division by zero by raising aZeroDivisionError
. - Example Usage: Variables
num1
andnum2
are initialized with example values, and the functions are called to perform addition, subtraction, multiplication, and division operations. - Output: The results of each operation are printed to the console using formatted strings (
f"..."
).
Usage:
To use this calculator program:
- Edit the values of
num1
andnum2
to input different numbers. - Run the Python script to perform addition, subtraction, multiplication, and division operations based on the input values.
explanation :
- Function Definitions:
- Functions (
add
,subtract
,multiply
,divide
) are explained with their roles in performing arithmetic operations in Python.
- Functions (
- Function Documentation:
- Each function includes docstring comments to describe their purpose, parameters, return values, and potential exceptions (like
ZeroDivisionError
).
- Each function includes docstring comments to describe their purpose, parameters, return values, and potential exceptions (like
- Exception Handling:
- The
divide
function handles the scenario wherenum2
is zero by raising aZeroDivisionError
.
- The
- Example Usage:
- Variables
num1
andnum2
are initialized with example values, and the functions are called to demonstrate addition, subtraction, multiplication, and division operations.
- Variables
- Output:
- The results of each operation (
sum_result
,difference
,product
,quotient
) are printed to the console using formatted strings (f"..."
).
- The results of each operation (
- Usage Instructions:
- Instructions are provided on how to use the calculator program by editing the input variables, running the Python script, and observing the results of arithmetic calculations.