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

23 % 3 the answer 2 cant understand

23%3 answer 2 cant understand

2 Answers

Ray Karyshyn
Ray Karyshyn
13,443 Points

Hi Fahad,

The modular operator (%) is similar to the division operator (/); however, modulus returns the remainder after dividing the given numbers.

For example, 4 mod 2 is 0, because 2 divides evenly into 4 and there is no remainder. Also, 5 mod 2 returns 1, because 5 divided by 2 is 2 with a remainder of 1.

So yes, 23 % 3 is 2, because 23 divided by 3 is 7 with a remainder of 2.

P.S. If you were looking to get the answer of 7, Python features the real floor division operator (//) -> 23 // 3 == 7

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

HI Fahad,

The modulo (%) operator is like a 'remainder' operator. If you divide 23 by 3 you get 7 remainder 2. Similarly 12 divided by 7 is 1 remainder 5, thus 12 % 7 == 5.

Cheers

Alex