Syllabus Lesson 3 of 239 · Python Foundations
Python Foundations

Numbers & Math

Python has two everyday number types. Whole numbers are ints (5, -3), and numbers with a decimal point are floats (3.5, 0.1).

qty = 5          # int
price = 3.5      # float
total = qty * price
print(total)     # 17.5

The operators

  • + add, - subtract, * multiply, / divide.
  • / always gives a float: 10 / 2 is 5.0.
  • // is floor division (divide and drop the remainder): 7 // 2 is 3.
  • % is modulo (the remainder left over): 7 % 2 is 1.
  • ** is power: 2 ** 3 is 8.

Modulo is handier than it looks. n % 2 == 0 tells you n is even, and % is how you wrap things around a clock.

print(17 // 5)   # 3
print(17 % 5)    # 2
print(3 ** 2)    # 9
Your turn

You have 17 cookies and 5 friends sharing them equally. Set each = 17 // 5 (cookies per friend) and leftover = 17 % 5 (cookies left over). Then print exactly Each gets 3, 2 left over using an f-string.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output