CodeStart / Python Course / Lesson 4
Lesson 4: Math Operators & Assignment Shortcuts
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→1because 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 toa = a + 1(Incrementsaby 1)b -= 5→ Equivalent tob = b - 5(Decrementsbby 5)c *= 3→ Equivalent toc = c * 3(Multipliescby 3)d /= 2→ Equivalent tod = d / 2(Dividesdby 2)e //= 4→ Equivalent toe = e // 4(Floor divideseby 4)f %= 3→ Equivalent tof = f % 3(Replacesfwith the remainder off / 3)
Want XP, levels and progress tracking? Play this lesson in the CodeStart game.
Play Lesson 4