Blog Post #12: Python’s Calculator: Mastering Arithmetic Operators (+, -, *, /)

In the last few posts, we’ve introduced the fundamental data types for numbers (Post #9), text (Post #10), and logic (Post #11). Now it’s time to put our number types, int and float, to work. One of the most basic tasks for any programming language is to perform calculations, and Python is excellent at it.

In this post, we’ll turn Python into a powerful calculator by learning the four basic arithmetic operators that form the foundation of most mathematical operations: addition, subtraction, multiplication, and division.

Addition (+)

The plus symbol (+) is used for addition, just like in regular math. It can be used to add integers, floats, or a mix of both.

apples = 10
oranges = 5
total_fruit = apples + oranges

print(total_fruit)

The output of this code will be 15.

Subtraction (-)

The minus symbol, or hyphen (-), is used for subtraction.

account_balance = 100.50
withdrawal = 20.25
new_balance = account_balance - withdrawal

print(new_balance)

The output here will be 80.25.

Multiplication (*)

In programming, the asterisk (*) is used for multiplication, not the × symbol you might be used to from math class.

price_per_item = 4.99
quantity = 3
total_cost = price_per_item * quantity

print(total_cost)

This will print 14.97.

Division (/)

The forward slash (/) is used for standard division. There is one very important rule to remember in Python: the division operator (/) always produces a float as its result. This is true even if the numbers divide evenly.

Let’s look at two examples.

# A division that results in a fraction
pizza_slices = 8
people = 3
slices_per_person = pizza_slices / people
print(slices_per_person)

# A division that results in a whole number
items = 10
boxes = 2
items_per_box = items / boxes
print(items_per_box)

The output will be:

2.6666666666666665
5.0

Notice that even though 10 divides by 2 perfectly, the result is 5.0 (a float), not 5 (an int). This consistency is a feature of the language that helps prevent subtle bugs in mathematical code.

Combining Operators and Order of Operations

You can combine these operators to create more complex expressions. Python follows the standard mathematical order of operations, often remembered by acronyms like PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction).

This means that multiplication and division are always performed before addition and subtraction.

# Python will do the multiplication first (2 * 3)
result = 5 + 2 * 3
print(result)

The output will be 11, not 21.

If you want to control the order of operations, you can use parentheses () to force an operation to be performed first.

# The parentheses force the addition to happen first
result_with_parens = (5 + 2) * 3
print(result_with_parens)

The output of this code will be 21.

What’s Next?

You can now use Python as a basic calculator, performing addition (+), subtraction (-), multiplication (*), and division (/). You also know that division always results in a float and that Python respects the standard order of operations.

These four operators are the workhorses of calculation, but Python has a few more specialized operators that are incredibly useful. In Post #13, we will explore these advanced operators, including floor division, modulus, and exponentiation, to give you even more mathematical power.

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