Numbers, operators, and conversion
Python uses integers for whole numbers and floating-point values for numbers with a fractional part.
How it works
Think of division as packing objects into boxes. With 17 objects and boxes that hold 5, // tells you how many full boxes fit and % tells you what is left. Use / for a ratio or average that can have a fractional part.
Floor division rounds toward negative infinity. For example, -7 // 3 gives -3. The remainder is 2 because -7 = 3 × (-3) + 2.
Walk through an example
Use / for division, // for floor division, and % for a remainder. 17 // 5 gives 3 full groups and 17 % 5 leaves 2 items.
What to watch for
Input text stays text until converted. int("12") creates an integer, but int("12.5") raises an error.
Code example
items = 23
print(items // 4, items % 4)Check your understanding
How many full bins can you fill, and how many items remain?
See the example and explanation
- Worked example
- Given
23 items, bins of 4 - Answer
5 full bins, 3 remaining
23 // 4 gives 5. Five bins use 20 items, so 23 % 4 leaves 3.