Course Content
Introduction to Python | Mobile-Friendly
Introduction to Python | Mobile-Friendly
Integer Division and Modulus
If you divide two integers, the result will not always be an integer. For example, if a trunk's capacity is 400 kg, then you can place only two 150kg items in it. The remaining capacity will be 100kg. This simple example shows us the real-life application of integer division and the remainder. The result of the integer division of 400 by 150 is 2, and the remainder is 100.
These operations can be performed in Python using the following operators:
//
- an integer division%
- a remainder
The example above can be represented in code in the following way:
# Number of 150kg items print(400//150) # Remaining capacity print(400%150)
Suppose you have $50 and are willing to buy as many snacks as possible. Each snack costs $15. Find out the maximum possible number of snacks and the leftover amount of change using integer division and the remainder.
Thanks for your feedback!