liveBook · Manning

Remainder operation

As explained in Get Programming: Learn to code with Python

A Python operator that computes the remainder after division of one numeric operand by another.

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

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines remainder with small arithmetic examples and applies it in a complete conversion program.
Examples
Uses 3 % 2, 6 % 4, and 123-minute conversion examples.
What it assumes
Assumes basic Python arithmetic and introduces the operator alongside integers, floats, division, and type conversion.
What you can do after
Apply it via Python to calculate values left after division and split totals into complete units and remainders.
How it connects
Connects remainder with division, integers, floats, type conversion, and mathematical operations.

Also explained in

Compare how 3 books explain remainder operation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.