Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Comparison Solution

why did he use the % sign for division i though that was used for a different function

that is the main part i struggled with is figuring out how to program the numbers right

3 Answers

Michael Hulet
Michael Hulet
47,912 Points

The percent sign (%) is known as the "modulus operator". It does performs a division operation, but instead of giving you the quotient of the division, it gives you a remainder. For example:

7 % 2 == 1
8 % 2 == 0
3 % 4 == 3

To explain each of those:

  • 2 goes into 7 three times, with 1 left over
  • 2 goes into 8 evenly 4 times
  • 4 does not go into 3, so there's 3 left over

To perform a regular division operation in Python, you'd use one of 2 operators, depending on the behavior you want:

  • A single forward slash (/) performs a division like you'd expect, returning a decimal number of the exact result
  • A double forward slash (//) performs a division operation, but always rounds the result down to the nearest integer

For example:

7 / 2 == 3.5
7 // 2 == 3

8 / 2 == 4
8 // 2 == 4

3 / 4 == 0.75
3 // 4 == 0

He used %(modulus) instead of/(float division operator) or //(integer division operator)to check if the number is divisible by either 3 (is_fizz) or 5(is_buzz) without leaving a remainder for instance 9%3 = 0 which means 9 is a fizz number because it was divisible by 3 without leaving a remainder or 23%3= 2 which means 20 is not a fizz number because it was divided by and left 2 as a remainder

Hi, I found this image https://www.onlinemathlearning.com/image-files/long-division-math.png which basically explains the long division, in the image, you will see that the number bottom is a "0", which means the number is an exact multiple of the divisor, otherwise, it would give a number other than "0" indicating that the dividend cannot be divided by the number that plays the divisor role.

In the case, we are examining if 3/3, 6/3 is something that would give us an exact number, (the results being 1 and 2, respectively) when we apply the 3%3 , we are checking for that number I mentioned earlier in the long division that shoul be equal to zero, giving place to either being a multiple or not of the condition provided, in case it is a number other than zero, then the number is not in compliance with the condition for the modulus.

Hope this helps a little to better understand the %(modulus) operator