Blog Post #14: Simple Math: Creating a Basic Calculator Program

It’s time to put theory into practice! Over the last several posts, we’ve learned about variables (Post #8), numbers (Post #9), and a whole suite of arithmetic operators (Post #12 and Post #13). Now, we’re going to combine all those concepts to build our very first tangible program: a simple calculator.

This mini-project will take two numbers and perform a series of calculations on them, printing the results neatly to the screen. It’s a fantastic way to solidify what you’ve learned so far and see how the individual building blocks come together.

Note: We haven’t learned how to get input from a user yet (that’s coming soon!), so for now, we will define our numbers directly in the code at the top of our script.

Step 1: Setting Up Our Numbers

The first step in our program is to create two variables to hold the numbers we want to work with. We can then refer to these variables by name throughout our script. Let’s use one integer and one float to see how they interact.

Create a new file called calculator.py and add the following lines:

Python

# --- Our Calculator's Input Numbers ---
# Feel free to change these values and see how the results change!
number_a = 15
number_b = 4.5

Step 2: Performing the Calculations

Now that we have our input numbers stored in variables, we can use the arithmetic operators we’ve learned to perform calculations. We will store the result of each calculation in its own descriptive variable.

Add the following code to your calculator.py file, right below the first two variables:

# --- Performing Basic Arithmetic ---
sum_result = number_a + number_b
difference_result = number_a - number_b
product_result = number_a * number_b
division_result = number_a / number_b

# --- Performing Advanced Arithmetic ---
floor_division_result = number_a // number_b
remainder_result = number_a % number_b
exponent_result = number_a ** 2  # Let's calculate number_a to the power of 2

Step 3: Displaying the Results

Our script has now performed all the calculations, but the results are just stored in memory. To see the answers, we need to print them to the console. We will use the print() function to display each result, adding a descriptive text string to make the output easy to read.

Add this final block of code to the end of your script:

# --- Displaying All the Results ---
print("--- Calculator Results ---")
print("Numbers:", number_a, "and", number_b)
print("--------------------------")

print("Sum:", sum_result)
print("Difference:", difference_result)
print("Product:", product_result)
print("Division:", division_result)

print("Floor Division:", floor_division_result)
print("Remainder:", remainder_result)
print("Exponentiation (a^2):", exponent_result)

The Complete Program and Your Turn to Experiment

Your final calculator.py file should look like this. When you run it from your terminal (python3 calculator.py), it will execute from top to bottom and print the formatted results.

# --- Our Calculator's Input Numbers ---
# Feel free to change these values and see how the results change!
number_a = 15
number_b = 4.5

# --- Performing Basic Arithmetic ---
sum_result = number_a + number_b
difference_result = number_a - number_b
product_result = number_a * number_b
division_result = number_a / number_b

# --- Performing Advanced Arithmetic ---
floor_division_result = number_a // number_b
remainder_result = number_a % number_b
exponent_result = number_a ** 2  # Let's calculate number_a to the power of 2

# --- Displaying All the Results ---
print("--- Calculator Results ---")
print("Numbers:", number_a, "and", number_b)
print("--------------------------")

print("Sum:", sum_result)
print("Difference:", difference_result)
print("Product:", product_result)
print("Division:", division_result)

print("Floor Division:", floor_division_result)
print("Remainder:", remainder_result)
print("Exponentiation (a^2):", exponent_result)

The real learning happens when you start experimenting. Go back to the top of the script and change the values of number_a and number_b. Try negative numbers, try zero, try two large numbers. See how the output changes. This is the best way to build an intuition for how operators work.

What’s Next?

Congratulations! You’ve successfully combined variables, data types, and operators to create a functional, multi-line program. This is a huge step forward!

Our calculator is great, but it’s not very interactive. Every time we want to use new numbers, we have to edit the code itself. Wouldn’t it be better if the program could ask us for numbers while it’s running? In Post #15, we will learn exactly how to do that by exploring the input() function, making our programs truly interactive.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment