Write a Python Program to Make a Simple Calculator

As a Python programmer, you may need to create a simple calculator program at some point.

A calculator program can be helpful when you need to perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

In this tutorial, we will be writing a Python program to create a simple calculator.


This calculator program will be able to perform the basic arithmetic operations of addition, subtraction, multiplication, and division.

Getting user input

The first step in creating a simple calculator program is to get user input.

We will be using the input() function to get the user input.

We will prompt the user to enter the first number, the operator (+,-,*,/), and the second number.

num1 = float(input("Enter the first number: "))
operator = input("Enter the operator (+,-,*,/): ")
num2 = float(input("Enter the second number: "))

Performing the arithmetic operation

After getting the user input, we will perform the arithmetic operation based on the operator entered by the user.

We will be using if-else statements to check the operator entered by the user and perform the corresponding arithmetic operation.

if operator == '+':
    result = num1 + num2
elif operator == '-':
    result = num1 - num2
elif operator == '*':
    result = num1 * num2
elif operator == '/':
    result = num1 / num2
else:
    print("Invalid operator entered")

Displaying the result

Finally, we will display the result to the user.

We will be using the print() function to display the result to the user.

print("Result: ", result)

The complete program to create a simple calculator in Python is as follows:

num1 = float(input("Enter the first number: "))
operator = input("Enter the operator (+,-,*,/): ")
num2 = float(input("Enter the second number: "))

if operator == '+':
    result = num1 + num2
elif operator == '-':
    result = num1 - num2
elif operator == '*':
    result = num1 * num2
elif operator == '/':
    result = num1 / num2
else:
    print("Invalid operator entered")

print("Result: ", result)

In conclusion, we have written a Python program to create a simple calculator.

This program can perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

This program can be helpful when you need to perform basic arithmetic operations quickly and easily.