โž— Arithmetic Operators

5th Grade ๐Ÿ’ป Coding

In code, you do math with symbols: `+` adds, `-` subtracts, `*` multiplies, `/` divides, `//` does whole-number division, `%` gives the remainder and `**` raises to a power.

Free ยท US Letter ยท no sign-up
Google Classroom Save to Pinterest Email me

In Python, 6 * 7 is 42, 10 / 4 is 2.5, 17 // 5 is 3 (how many times 5 fits into 17), and 10 % 3 is 1 (the remainder). 2 ** 3 is 8, which is 2 to the power of 3. Multiplication uses an asterisk because keyboards donโ€™t have a ร— key.

The order of operations in code is the same as in math: multiplication and division come before addition and subtraction, and parentheses come first of all. The remainder operator % is especially handy. An even number is one whose remainder when divided by 2 is 0, so you can check it with n % 2 == 0.

For example

  • To share 17 pieces of candy among 5 kids: 17 // 5 gives each kid 3, and 17 % 5 says 2 are left over.
  • 2 + 3 * 4 is 14, not 20, because the multiplication happens first.

๐Ÿ“Œ Remember: * multiply, / divide, // whole division, % remainder, ** power.

Quick quiz

  1. What is 10 % 3?

  2. What is 2 ** 3?

  3. What is 2 + 3 * 4?

๐Ÿ”Ž Ask Thinkapedia another question