Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 13

Arithmetic Operations in Python

This lesson is built with resources from youtube.com, fullstackfoundations.com and 1 more site.

Hello! Welcome to our third module, where we'll explore Operators and Expressions.

In our last lesson, we saw how operators like + can behave differently depending on the data types they're working with—sometimes adding numbers, other times joining strings. This was a deep dive into JavaScript's automatic type coercion.

Today, we'll build on that by formally exploring the fundamental tools for performing calculations. Our learning outcome is to *apply arithmetic operators (+, -, , /, %) to perform calculations. These operators are the mathematical bedrock of any program, from simple counters to complex physics simulations. We'll cover the basic operators, two special ones that are very useful, and the all-important rules that govern the order in which calculations happen.

The Basic Arithmetic Operators

In JavaScript, just like in mathematics, we have operators to perform calculations. You're already familiar with most of these from school.

The core arithmetic operators are:

  • Addition (+): Adds two numbers.
  • Subtraction (-): Subtracts one number from another.
  • Multiplication (*): Multiplies two numbers.
  • Division (/): Divides one number by another.

Let's watch a short video that introduces these and a few others we'll cover next.

JavaScript Arithmetic Operators – Everything You Need to Know

The video 'JavaScript Arithmetic Operators – Everything You Need to Know' by Future Fullstack provides a clean and engaging introduction to these concepts.

Please watch from 01:46 to 03:12. The video presents a clear table summarizing the main arithmetic operators. Pay attention to the operator symbol and its purpose.


The Remainder and Exponentiation Operators

The video introduced two more very useful operators:

  1. Remainder / Modulus (%): This operator doesn't give you the result of a division, but rather the remainder left over. For example, 10 % 3 is 1, because 3 goes into 10 three times (making 9), with a remainder of 1. This is incredibly useful for tasks like checking if a number is even or odd. If number % 2 is 0, the number is even!
*This slide visually breaks down the modulus operator, showing that `13 % 5` results in `3`, the remainder of the division.*

2. Exponentiation (**): This operator raises the first number to the power of the second number. For example, 2 ** 4 is 16 (2 × 2 × 2 × 2). It's a modern and readable way to perform this calculation.

For a great written summary of these operators, you can refer to the following resource.

Basic operators, maths

The article 'Basic operators, maths' from javascript.info is an excellent and concise reference. It clearly explains the remainder and exponentiation operators with code examples.

Read the section titled 'Maths' (you can skip the parts about unary and binary operators for now). Focus on the explanations for 'Remainder %' and 'Exponentiation **'.

Test your understanding!

What will be logged to the console for each of these expressions? Try to solve them before running the code.

console.log(100 / 4);
console.log(3 * 7);
console.log(15 % 4);
console.log(5 ** 3);
console.log(14 % 2);
Show answer
  1. 25 - The result of dividing 100 by 4.
  2. 21 - The result of multiplying 3 by 7.
  3. 3 - 4 goes into 15 three times (12), with a remainder of 3.
  4. 125 - This is 5 to the power of 3, which is 5 * 5 * 5.
  5. 0 - 2 goes into 14 exactly seven times, with a remainder of 0. This indicates that 14 is an even number.

Order of Operations: Operator Precedence

What happens when you have multiple operators in one expression, like 5 + 10 * 2? Does JavaScript calculate 5 + 10 first, or 10 * 2 first?

The answer is governed by operator precedence. Just like in math class, JavaScript follows a specific order of operations, often remembered by the acronym PEMDAS or BIDMAS:

  1. Parentheses ()
  2. Exponents **
  3. Multiplication * and Division / (from left to right)
  4. Addition + and Subtraction - (from left to right)

This means that multiplication and division are always performed before addition and subtraction. So, in 5 + 10 * 2, JavaScript will calculate 10 * 2 first, giving 20, and then calculate 5 + 20, for a final result of 25.

If you want to override this default order, you use parentheses (). For example, (5 + 10) * 2 would force the addition to happen first, resulting in 15 * 2, which is 30.

Since you prefer visual explanations for deep theory, the next segment of the video provides an excellent, animated breakdown of this concept.

JavaScript Arithmetic Operators – Everything You Need to Know

Let's return to the 'JavaScript Arithmetic Operators' video to see a fantastic visual explanation of operator precedence.

Watch the segment from 07:26 to 11:56. The creator does a great job of walking through complex examples step-by-step, showing how the BIDMAS/PEMDAS rules apply in code.

Test your understanding!

What is the final value of result?

let result = 30 - 4 * 2 ** 2 / 2 + (5 + 3);

Work through it step-by-step using the rules of precedence.

Show answer

Let's break it down:

  1. Parentheses: (5 + 3) becomes 8.
    • Expression is now: 30 - 4 * 2 ** 2 / 2 + 8
  2. Exponents: 2 ** 2 becomes 4.
    • Expression is now: 30 - 4 * 4 / 2 + 8
  3. Multiplication/Division (left to right):
    • First, 4 * 4 becomes 16. Expression is now: 30 - 16 / 2 + 8
    • Next, 16 / 2 becomes 8. Expression is now: 30 - 8 + 8
  4. Addition/Subtraction (left to right):
    • First, 30 - 8 becomes 22. Expression is now: 22 + 8
    • Finally, 22 + 8 becomes 30.

The final result is 30.


Practical Application: A BMI Calculator

Theory is great, but let's see these operators in a practical scenario. A Body Mass Index (BMI) calculator is a perfect example. The formula is:

BMI = weight (kg) / height (m)²

This formula requires both division (/) and exponentiation (**). Let's see how to implement this in JavaScript.

JavaScript Arithmetic Operators – Everything You Need to Know

The final part of the video we've been watching builds a simple BMI calculator, showing how these operators are used in a real project.

Watch from 13:22 to 16:05. Notice how the code combines variables with arithmetic operators to calculate and store the result. This is a very common pattern in programming.

The key line of code in that example is:

let bmi = weightInKg / heightInMeters ** 2;

Because of operator precedence, JavaScript correctly calculates heightInMeters ** 2 first, and then performs the division. This is exactly what we need for the formula to work!


Practice Problems: Combining Arithmetic and Type Coercion

To really solidify your understanding, let's tackle a few problems that combine today's lesson with what we learned about type coercion in the previous lesson. These are tricky but are excellent for building a deep understanding.

Basic operators, maths

The 'Type conversions' task from the javascript.info article we looked at earlier presents some expressions that might surprise you. Try to solve them!

Navigate to the task titled 'Type conversions'. Predict the result of each expression before looking at the provided answers. Pay close attention to the ones involving arithmetic operators like 6 / "3", "2" * "3", and " -9 " - 5.

The key takeaway from those exercises is that all arithmetic operators except for addition (+) will try to convert non-numeric values into numbers before performing the calculation. The + operator is special because it will perform string concatenation if either operand is a string.


Conclusion

Great job today! You've learned how to use the fundamental arithmetic operators in JavaScript to perform calculations, which is a core skill for any programmer.

Key Takeaways:

  • The basic arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).
  • The remainder (%) operator finds the remainder of a division, while the exponentiation (**) operator calculates powers.
  • Operator precedence (PEMDAS/BIDMAS) determines the order of calculations. Multiplication/division happens before addition/subtraction.
  • You can use parentheses () to control the order of operations explicitly.
  • Except for +, all arithmetic operators will attempt to convert values to numbers before calculating.

Next Up:

We've focused on how these operators work with numbers. In the next lesson, we'll dive deeper into how the + operator works with strings to perform concatenation. We will also learn about template literals, a modern and much more powerful way to construct strings in your JavaScript code, which is essential for building dynamic web applications with frameworks like Next.js.