Splitting minutes and wrapping days
The third line proves the rule that ties // and % together. type() tells you the kind of a value (Topic 1.6): 10 / 2 is a float, 10 // 2 is an int.
An int is a whole number, positive, negative or zero, with no size limit. Python adds, subtracts and multiplies it exactly, and has special operators for division with a quotient (//), a remainder (%) and powers (**).
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
A shop sells pens in packs of 12. You have 100 pens. Print how many full packs you can make and how many pens are left over.
Print the last digit of 4729 and the number with its last digit removed, using % and //.
A clock shows hour 10. Print the hour on the clock 5 hours later, on a 12-hour clock where the hours are 0 to 11.
Explain it without notes
What is the difference between /, // and %? Give an example for each.
Why is -17 // 5 equal to -4, and what is -17 % 5?
What does it mean that Python integers are unlimited, and what is the trade-off?
How would you turn 3725 seconds into hours, minutes and seconds?
Expected output
2 h 15 min
True
day after 3 days: 1
3 -4 -4
<class 'float'> <class 'int'>
<class 'int'> <class 'float'>