Python Simple Calculator Example
A simple calculator is a great beginner Python project. It helps you practice several important basics in one small script:
- getting user input
- storing values in variables
- converting text to numbers
- using
if,elif, andelse - doing basic math
- handling simple errors
In this example, you will build a calculator that asks for two numbers and an operator, then prints the result.
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
if operator == "+":
print(num1 + num2)
elif operator == "-":
print(num1 - num2)
elif operator == "*":
print(num1 * num2)
elif operator == "/":
if num2 == 0:
print("Cannot divide by zero")
else:
print(num1 / num2)
else:
print("Invalid operator")
Use this as the main working example. The rest of the page explains each part step by step.
What this example teaches
This small project shows you how to:
- get values from the user with
input() - convert text input into numbers with
float() - choose an action with
if,elif, andelse - perform
+,-,*, and/operations - handle invalid operators and division by zero
What the calculator should do
The calculator should:
- ask for the first number
- ask for the operator
- ask for the second number
- run the correct math operation
- print the result or a clear error message
Step 1: Read user input
The first part of the program gets values from the user.
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
Important idea: input() always returns text
Even if the user types 12, Python reads it as a string, not a number.
That is why the code uses float() around the input() calls for num1 and num2.
input()gets the user's typed valuefloat()converts that text into a number with decimals
For example:
value = input("Enter a number: ")
print(type(value))
Output:
<class 'str'>
After conversion:
value = float(input("Enter a number: "))
print(type(value))
Output:
<class 'float'>
If you want more practice with this, see how to get user input in Python and how to convert user input to numbers in Python.
Step 2: Check the operator
Next, the program checks which operator the user typed.
if operator == "+":
print(num1 + num2)
elif operator == "-":
print(num1 - num2)
elif operator == "*":
print(num1 * num2)
elif operator == "/":
print(num1 / num2)
else:
print("Invalid operator")
This works by comparing the operator variable to different strings.
- if the user enters
"+", the program adds - if the user enters
"-", the program subtracts - if the user enters
"*", the program multiplies - if the user enters
"/", the program divides - anything else goes to
else
The else part is useful for unsupported input such as:
%?add
Step 3: Handle division safely
Division needs one extra check.
If you try to divide by zero, Python raises an error. To avoid that, check num2 before dividing.
elif operator == "/":
if num2 == 0:
print("Cannot divide by zero")
else:
print(num1 / num2)
This makes the program friendlier for beginners and users.
Without the check, code like this would fail:
print(8 / 0)
That causes a ZeroDivisionError. If you want to understand that error in more detail, see ZeroDivisionError: division by zero fix.
Full calculator example
Here is the full program again:
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
if operator == "+":
print(num1 + num2)
elif operator == "-":
print(num1 - num2)
elif operator == "*":
print(num1 * num2)
elif operator == "/":
if num2 == 0:
print("Cannot divide by zero")
else:
print(num1 / num2)
else:
print("Invalid operator")
How it works
num1stores the first numberoperatorstores the math symbolnum2stores the second number- the
ifstructure chooses the correct calculation - the result is printed to the screen
Expected output examples
Here are some sample runs.
Example 1
Enter first number: 12
Enter operator (+, -, *, /): +
Enter second number: 3
15.0
Example 2
Enter first number: 10
Enter operator (+, -, *, /): /
Enter second number: 2
5.0
Example 3
Enter first number: 8
Enter operator (+, -, *, /): /
Enter second number: 0
Cannot divide by zero
Example 4
Enter first number: 4
Enter operator (+, -, *, /): ?
Enter second number: 2
Invalid operator
Beginner improvements
Once this version works, you can improve it in small steps.
Try one of these:
- round the result with
round() - repeat the calculator in a loop
- let users quit with
q - use
int()instead offloat()if you only want whole numbers
For example, rounding the result:
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
if operator == "/":
if num2 == 0:
print("Cannot divide by zero")
else:
print(round(num1 / num2, 2))
This prints the division result rounded to 2 decimal places.
When this example is useful
This project is useful:
- as a first small Python project
- to practice conditionals and input
- to combine multiple beginner concepts in one script
- to prepare for larger menu-based programs
It is a good next step after learning variables, input(), and basic if statements.
Common mistakes
Beginners often run into the same problems when building this calculator.
Forgetting to convert input() values to numbers
This is very common. If you skip float(), the values stay as strings.
Wrong:
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print(num1 + num2)
If the user enters 2 and 3, this prints:
23
That happens because Python joins the strings instead of adding numbers.
Using = instead of == in an if statement
Wrong:
if operator = "+":
print(num1 + num2)
Use == when comparing values.
Correct:
if operator == "+":
print(num1 + num2)
Not checking for division by zero
If num2 is 0, division will fail unless you handle it first.
Typing an unsupported operator
Your program only supports:
+-*/
If the user types something else, the program should show Invalid operator.
Misspelling variable names
For example, writing nub2 instead of num2 will cause errors or wrong behavior.
FAQ
Why use float() instead of int()?
float() allows decimal numbers like 2.5. int() only works for whole numbers.
Why does input() need conversion?
input() returns a string. Math needs numbers, so convert with int() or float().
Why does division return a decimal?
In Python, / returns a float result even when both numbers are whole numbers.
Can I make the calculator run again without restarting?
Yes. Put the code inside a while loop and ask the user whether to continue.
See also
- Python
input()function explained - Python
float()function explained - Python
if,else, andelifexplained - How to convert user input to numbers in Python
- ValueError: invalid literal for int() with base 10 fix
- Python number guessing game example
If you want more practice, try building a second version of this calculator with a loop, better input validation, or extra operators.