A Simple Calculator program in Python to practice basic programming concepts like user input, conditionals, and arithmetic operations. In this Python project, you’ll create a calculator that can perform four basic operations: Addition (+), Subtraction (-), Multiplication (*), and Division (/).
This calculator takes two numbers from the user and an operator to perform the calculation. It also handles the error of division by zero. It’s beginner-friendly and easy to understand. It uses if-elif-else conditions.
Simple Calculator: Beginner Project with Code
print("\n-------Welcome to the Simple Calculator-------\n")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operator = input("Choose an operator (+, -, *, /): ")
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 != 0:
result = num1 / num2
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid operator. Please use +, -, *, or /.")
print(f"Result: {num1} {operator} {num2} = {result}")
Output:
-------Welcome to the Simple Calculator-------
Enter the first number: 22
Enter the second number: 5
Choose an operator (+, -, *, /): *
Result: 22.0 * 5.0 = 110.0
Creating a Simple Calculator in Python helps you understand the core structure of a Python program. It’s a must-do mini project for beginners learning Python.