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.5The operators
+add,-subtract,*multiply,/divide./always gives a float:10 / 2is5.0.//is floor division (divide and drop the remainder):7 // 2is3.%is modulo (the remainder left over):7 % 2is1.**is power:2 ** 3is8.
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) # 9Your 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.
Lesson complete. Nice work.
Code · runs in your browser
Output
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.