Computer Science
Senior High
Developing a Program in Python: Building a Simple Calculator
Topic: Y2 Developing a program in a programming language, e.g. a simple calculator
How to write a real program in Python, step by step, using a calculator as the example: print, input, variables and arithmetic, turning an algorithm into code ; decisions, loops, functions, recursion and a complete calculator; functions for real problems, rounding and floating-point precision.
What's inside
A calculator is a perfect first real program. You know exactly what it should do, you can check every answer, and you can make it smarter step by step. This note builds a calculator in Python across the three years: a basic version first, then one that makes decisions and repeats, and finally one that handles decimals correctly for real-world use.
SUMMARIZED NOTES
Year 1: Python basics: print(), variables and data types, input(), arithmetic operators, comments, and turning a simple algorithm into code.
Year 2: Conditional statements (if, elif, else), loops (for and while), functions, recursion, and building a complete simple calculator.
Year 3: Using functions to solve real-world problems, and handling floating-point precision with round().
Year 1: Python basics: input, process, output
Every simple program follows the pattern input → process → output, like a juice machine: oranges go in, the machine squeezes, and juice comes out. Python gives us a tool for each part.
Output: print()
print() shows something on the screen. print("Welcome to my calculator") displays that message. You can print text and variables together: print("The answer is", answer).
Storing values: variables
A variable is a named container for a value. x = 529 puts 529 in a box labelled x. Choose meaningful names (cost, not number1); a name can't start with a number or contain spaces, so use underscores or camelCase (first_name or firstName).
Input: input()
input() asks the user to type something while the program runs. Everything typed comes in as text, so to do arithmetic you must convert it: int() for whole numbers, float() for decimals.
Process: arithmetic operators
Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 7 + 3 | 10 |
- | Subtract | 7 - 3 | 4 |
* | Multiply | 7 * 3 | 21 |
/ | Divide | 7 / 2 | 3.5 |
** | Power (exponent) | 10 ** 3 | 1000 |
Comments
Anything after a # is a comment: a note for humans that Python ignores. Comments explain what the code does, making it easier to read and fix later.
Open the full resource free with a TudHub account.
More on Developing a program in a programming language, e.g. a simple calculator
Other resources you can find on TudHub.