Lesson 4: Math Operators & Assignment Shortcuts

Module 2: Variables, Types & Operators · about 8 min · free

In Python, you can perform standard arithmetic operations as well as specialized division and shortcut operations. Here is a master list of all the essential operators you will use when writing code.

Basic Arithmetic Operators

  • + Addition: Adds two numbers together. (e.g., 5 + 2 → 7)
  • - Subtraction: Subtracts the right number from the left. (e.g., 5 - 2 → 3)
  • * Multiplication: Multiplies two numbers. (e.g., 5 * 2 → 10)
  • / Standard Division: Divides numbers and always results in a float decimal. (e.g., 5 / 2 → 2.5)
  • // Integer Division (Floor Division): Divides numbers and rounds down to the nearest whole number, discarding the remainder. (e.g., 5 // 2 → 2)
  • % Modulus: Returns the remainder of a division. (e.g., 5 % 2 → 1 because 5 divided by 2 is 2 with 1 left over)
  • ** Exponentiation: Raises a number to the power of another. (e.g., 5 ** 2 → 25)

Assignment Shortcuts (Operator Simplifiers)

When updating a variable's value based on its current value, you can write shorthand statements instead of repeating the variable name on both sides of the equal sign:

  • a += 1  →  Equivalent to a = a + 1 (Increments a by 1)
  • b -= 5  →  Equivalent to b = b - 5 (Decrements b by 5)
  • c *= 3  →  Equivalent to c = c * 3 (Multiplies c by 3)
  • d /= 2  →  Equivalent to d = d / 2 (Divides d by 2)
  • e //= 4  →  Equivalent to e = e // 4 (Floor divides e by 4)
  • f %= 3  →  Equivalent to f = f % 3 (Replaces f with the remainder of f / 3)

Want XP, levels and progress tracking? Play this lesson in the CodeStart game.

Play Lesson 4