Lesson 8 of 18 · 4 min
Arithmetic Operators — Doing Math
The WHY — Why Learn Operators?
Every calculation in data science — average grade, total sales, percentage growth — uses arithmetic operators. Python can be your most powerful calculator.
All 7 Operators Explained
| Operator | Name | Example | Result | Plain English |
|---|---|---|---|---|
+ | Addition | 5 + 3 | 8 | Add together |
- | Subtraction | 10 - 4 | 6 | Subtract |
* | Multiplication | 6 * 7 | 42 | Multiply (asterisk, not ×) |
/ | Division | 7 / 2 | 3.5 | Divide — ALWAYS gives decimal |
// | Floor Division | 7 // 2 | 3 | Divide, throw away the decimal |
% | Modulo | 7 % 2 | 1 | What is the REMAINDER? |
** | Exponent | 2 ** 8 | 256 | Raise to a power (2⁸) |
Live Examples with Every Operator
a = 17 # first number for all examples below b = 5 # second number for all examples below print(a + b) # addition: 17 + 5 print(a - b) # subtraction: 17 - 5 print(a * b) # multiplication: 17 × 5 print(a / b) # division: 17 ÷ 5 (always decimal/float) print(a // b) # floor division: 17 ÷ 5 then drop the decimal print(a % b) # modulo: remainder when 17 is divided by 5 print(a ** b) # exponent: 17 to the power of 5
Output: 22 12 85 3.4 3 2 1419857
Understanding Floor Division and Modulo with Cookies
Imagine you have 17 cookies to share among 5 friends:
cookies = 17 # total cookies we have
friends = 5 # number of friends to share with
each_gets = cookies // friends # how many WHOLE cookies per friend?
leftover = cookies % friends # how many cookies are left over?
print("Each friend gets:", each_gets) # display cookies per friend
print("Leftover:", leftover) # display remaining cookies
Output: Each friend gets: 3 Leftover: 2
17 ÷ 5 = 3 remainder 2. The // gives you the 3, and % gives you the 2.
Super Useful: Checking Even or Odd with %
number = 17 # the number to test
# any even number divided by 2 has remainder 0
# any odd number divided by 2 has remainder 1
if number % 2 == 0: # check if remainder is zero (no leftover)
print("Even") # no remainder = divisible by 2 = even
else:
print("Odd") # has remainder = not perfectly divisible = odd
Output: Odd
The Crucial Difference Between / and //
# / always returns a float (decimal), even if the answer is a whole number print(10 / 2) # result is 5.0 (not 5!) — always a float print(7 / 2) # result is 3.5 # // drops the decimal part entirely (floors it — rounds toward negative infinity) print(7 // 2) # result is 3 (decimal .5 is thrown away) print(10 // 3) # result is 3 (decimal .333 is thrown away) print(type(10 / 2)) # <class 'float'> — always float print(type(10 // 2)) # <class 'int'> — always int (when both operands are int)
Output: 5.0 3.5 3 3 <class 'float'> <class 'int'>
Shortcut Assignment Operators
score = 100 # start at 100
score += 10 # same as: score = score + 10 (now 110)
score -= 5 # same as: score = score - 5 (now 105)
score *= 2 # same as: score = score * 2 (now 210)
score //= 7 # same as: score = score // 7 (now 30)
print("Final score:", score) # display the final result
Output: Final score: 30
