In this tutorial, we will learn how to create a basic calculator using Python. We will use simple arithmetic operations such as addition, subtraction, multiplication, and division.
First, let’s create a new Python file and name it calculator.py. Open the file in your preferred text editor or IDE and let’s begin.
In Python, we can use classes to define objects and their behaviors. Start by creating a Calculator class:
class Calculator:
def __init__(self):
pass
In the __init__ method, we will initialize any necessary variables.
Let’s add a function to perform addition. Add the following method to the Calculator class:
def add(self, num1, num2):
return num1 + num2
The add method takes in two numbers (num1 and num2) and returns their sum.
Similarly, let’s implement subtraction functionality. Add the following method to the Calculator class:
def subtract(self, num1, num2):
return num1 - num2
The subtract method takes in two numbers (num1 and num2) and returns their difference.
Next, let’s add multiplication functionality. Add the following method to the Calculator class:
def multiply(self, num1, num2):
return num1 * num2
The multiply method takes in two numbers (num1 and num2) and returns their product.
Lastly, let’s implement division functionality. Add the following method to the Calculator class:
def divide(self, num1, num2):
if num2 == 0:
return "Cannot divide by zero"
return num1 / num2
The divide method takes in two numbers (num1 and num2) and checks if the divisor (num2) is zero. If it is, the method returns an error message. Otherwise, it returns the quotient of the division.
Now that we have implemented all the necessary functionality, let’s test our calculator. Add the following code at the end of the calculator.py file:
# Create an instance of the Calculator class
calculator = Calculator()
# Test addition
print(calculator.add(5, 3))
# Test subtraction
print(calculator.subtract(5, 3))
# Test multiplication
print(calculator.multiply(5, 3))
# Test division
print(calculator.divide(5, 3))
Save the file and run it. You should see the following output:
8
2
15
1.6666666666666667
Congratulations! You have successfully created a simple calculator in Python.
In this tutorial, we learned how to create a basic calculator using Python. We implemented addition, subtraction, multiplication, and division functionalities. You can expand on this project by adding more operations or improving the user interface.
I hope you found this tutorial helpful. Happy coding!
Remember to replace the title, date, and description fields in the front matter with appropriate values. You can also customize the categories, tags, and type fields as per your requirements.