The remainder operation, written %, finds what is left after one number is divided by another. For example, 3 % 2 is 1 because one 2 fits into 3, with 1 left over. section 5.3
Convert minutes into hours and minutes
minutes_to_convert = 123 hours_part = int(minutes_to_convert / 60) minutes_part = minutes_to_convert % 60 The program produces 2 whole hours and 3 remaining minutes. Dividing by 60 gives the hours; % 60 gives the minutes left over. section 6.3 section 6.5
Use % to:
- Find what remains after division, such as 6 % 4, which gives 2. section 5.3
- Separate a total into complete groups and the amount left over, as with minutes and hours. section 6.3
Listing 6.2. Convert minutes to hours and minutes using the remainder
minutes_to_convert = 123 #1
hours_decimal = minutes_to_convert/60 #2
hours_part = int(hours_decimal) #3
minutes_part = minutes_to_convert%60 #4
print("Hours")
print(hours_part)
print("Minutes")
print(minutes_part)
A complete Python listing shows the remainder operation inside a small minutes-to-hours conversion program. section 6.5


