A Python project – A calculator
Lesson 1 of 15 · Python projects
A more detailed version:
The calculator is already covered in a lesson of the basic course. See: Mini projects: a calculator and exercises.
The calculator is already covered in a lesson of the basic course. See: Mini projects: a calculator and exercises.
In this project we will make a simple calculator in Python that can do the basic mathematical operations.
This project is excellent for beginners because it combines several basic Python concepts.
In this project we will use:
- entering data from the keyboard
- if conditions
- mathematical operations
- controlling the flow of the program
How the calculator works
The program asks the user to enter two numbers and a mathematical operation.
Then Python calculates the result and shows it on the screen.
An example of the program running
Python calculator
Enter the first number: 10
Enter the operation (+ - * /): *
Enter the second number: 5
Result: 50.0
The program code
print("Python calculator")
a = float(input("Enter the first number: "))
operation = input("Enter the operation (+ - * /): ")
b = float(input("Enter the second number: "))
if operation == "+":
result = a + b
elif operation == "-":
result = a - b
elif operation == "*":
result = a * b
elif operation == "/":
result = a / b
else:
print("Unknown operation")
exit()
print("Result:", result)
How this code works
- input() takes data from the user
- float() converts text into a number
- if / elif checks which operation the user chose
- the program calculates the result
Try to extend the program by
adding more operations or repeating the program
in a loop.
Possible improvements to the project
- add support for more operations
- make the calculator run in a loop
- add error checking
- make a graphical calculator
Next project
If you have successfully made the calculator, the next project is a small Python game.
How to make the project your own:
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.
