โ 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.
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 // 5gives each kid 3, and17 % 5says 2 are left over. 2 + 3 * 4is 14, not 20, because the multiplication happens first.
๐ Remember: * multiply, / divide, // whole division, % remainder, ** power.
Quick quiz
What is 10 % 3?
What is 2 ** 3?
What is 2 + 3 * 4?