Back to Blog
Lesson 3 of the Python: Programming from Zero with Python course
PythonJuly 13, 20264 min read

Arithmetic Operations: Mastering Math in Python

Master arithmetic operations in Python. Learn to use operators for addition, subtraction, multiplication, and division, and control order of operations.

pythonprogrammingarithmeticmathoperatorsbeginners

Previously in this course, we covered Variables and Data Types: Python Programming from Zero, where you learned how to store and label information in your scripts. Now that you can hold data in memory, it's time to process it.

In this lesson, we’ll move from simply storing values to performing calculations. Whether you are building a data-processing CLI or a complex API, understanding how to handle numbers is a core skill for any backend engineer.

The Basics of Math in Python

Python functions as a powerful calculator right out of the box. You use standard symbols—called arithmetic operators—to perform math on integers (whole numbers) and floats (numbers with decimal points).

Here are the primary operators you’ll use daily:

OperationOperatorExample
Addition+5 + 2 (Result: 7)
Subtraction-5 - 2 (Result: 3)
Multiplication*5 * 2 (Result: 10)
Division/5 / 2 (Result: 2.5)

Performing Calculations in Your Code

Let’s advance our running project—a data-processing CLI. Imagine we need to calculate the total cost of items or compute an average from user inputs. You can perform these calculations directly within your code and assign the results to variables.

PYTHON
# Calculating a simple total price
item_price = 19.99
quantity = 3
tax_rate = 0.05

subtotal = item_price * quantity
tax_amount = subtotal * tax_rate
total_price = subtotal + tax_amount

print(f"Total: {total_price}")

In this example, Python evaluates the right side of the = operator first, performs the arithmetic, and then stores the final value in the variable on the left.

Understanding Order of Operations

Just like in algebra, Python follows the PEMDAS rule (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). If you write a complex expression, Python doesn't necessarily evaluate it from left to right.

Consider this calculation: result = 10 + 5 * 2

If you read it left-to-right, you might expect 15 * 2 = 30. However, Python prioritizes the multiplication. It calculates 5 * 2 = 10 first, then adds the 10 to get 20.

To override this, or to make your code more readable, always use parentheses:

PYTHON
# Force addition before multiplication
result = (10 + 5) * 2  # Result is 30

Hands-on Exercise

Open your VS Code environment (which you configured in Setting Up the Python Environment: A Complete Guide for Beginners) and create a new file named math_test.py.

  1. Define two variables: width = 10 and height = 5.
  2. Calculate the area of a rectangle using width * height and store it in a variable called area.
  3. Calculate the perimeter using (width + height) * 2 and store it in perimeter.
  4. Print both results to the console.

Common Pitfalls

Even experienced engineers run into these two common issues when dealing with math:

  • Integer Division: In Python 3, the / operator always returns a float (e.g., 4 / 2 becomes 2.0). If you specifically need an integer result, you would need to use floor division (//), but be careful as it rounds down.
  • Floating Point Precision: Computers store decimal numbers in binary, which can lead to tiny precision errors. For instance, 0.1 + 0.2 might result in 0.30000000000000004 rather than exactly 0.3. For simple CLI tools, this is usually fine, but if you are building a financial application, you will eventually want to use the decimal module.

FAQ

Can I mix integers and floats in the same calculation? Yes. Python automatically "promotes" the result to a float if any part of the calculation involves a float (e.g., 5 + 2.0 results in 7.0).

What if I need more advanced math? For basic arithmetic, the built-in operators are sufficient. For complex functions like square roots, trigonometry, or logarithms, you will eventually use the math module, which we will cover later in the course.

Does whitespace matter around operators? No, 5+2 and 5 + 2 are both valid. However, PEP 8 (the Python style guide) recommends using spaces around operators to make your code easier to read.

Recap

In this lesson, we covered the essential arithmetic operators (+, -, *, /) and the importance of using parentheses to manage the order of operations. You now have the ability to perform calculations within your scripts, a foundational skill for the data-processing tools we are building.

Up next: Input and Output — we will learn how to capture dynamic data from users using the input() function.

Similar Posts