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

Daniel Reis
Daniel Reis
1,050 Points

I got lost at % . Can someone expand on how/why/when to use %?

Why would one use % instead of / ? I'm not sure if it's my lack of maths here but I just couldn't work out how one would see if a number is divisible by another. Can someone give other options on how this could have been done to see if I understand it better?

Thanks a mil!

2 Answers

Santiago Posada
Santiago Posada
5,189 Points

Division will be done with /, Example 4/2 = 2

Modulo operation will give you the remainder, meaning if your are able to divide a number by another will give you 0 Example, You can divide every EVEN number by 2, So if you use 10%2 will be equal to 0 since 2 "FITS" in 10 an exact number of times. In this case it "Fits" 5 times. Same goes for the other numbers. 21%3 =0 but 21/3= 7

For this challenge, '''if number %3 ==0: ''' means the number can be divided by 3, since 3 fits an exact number of times in that number.

Hope this helps!

Steven Parker
Steven Parker
231,268 Points

The % operator gives you the remainder of the division. So it will always be 0 when you use it on numbers where one is evenly divisible by the other.