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 trialMariya Shklyar
1,022 PointsLoops and if conditions if num % 2 == 0
In th Round and Round video, in about 3:00 Kenneth talking about how to use 'if' in 'for' loop. I can't undestand what the condition is. I thought we use '/' for division. What is '%'? Why and what is == 0? How do use concept of index here? If responding, please consider that Python is the first language I'm learning.
2 Answers
Sky Lu
1,260 PointsHi Mariya,
The operator % here is for division, it will return remainder.
In this case:
if num % 2 == 0
means number divide 2 , and the remainder is 0, then it will print out.
in the video, num got [1, 2, 3, 4]
1 รท 2 the remainder is 1
2 รท 2 the remainder is 0
3 รท 2 the remainder is 1
4 รท 2 the remainder is 0
That's why it print out is 2 and 4
if you make the num to [1, 2, 3, 4, 5, 6]
the result will be 2, 4, 6
ivettelindsay
4,010 PointsThank you :-)
Michael Rathbun
1,624 PointsYes ok this is what I was looking for as well. thank you for asking Mariya ! Kenneth is a great teacher, but sometimes he buzzes through things like this and I have to turn the speed down and stop the video to ingest it properly.
tobiaskrause
9,160 Pointsfor num in [1, 2, 3, 4]:
if num % 2 == 0:
print(num)
he just checked which of these numbers are even. Because the if statement will trigger for each element in the array. So for the number 2 it would print 2...because the statement is true...and for the number 3 it would print nothing cause the statement is false.
john larson
16,594 PointsTobias, I get the basic concept that % is used to check if a number is even. But when I run your numbers I get different results. I get 10%4 = 2. I havn't seen the % used like this yet, but I've heard mention of it. I'm hoping to understand
tobiaskrause
9,160 PointsSry Sry. I am at work...my brain is not focused at TH :) So modulo is just the reminder of a devision. f. e. 20 % 6 = 2 because you can just get 18 and not 24...and 20-18 = 2
john larson
16,594 PointsOk, I see how it works now. returns 0 = even. returns 1 = odd. Did I get that right?
tobiaskrause
9,160 PointsMaybe this blog post might help you https://betterexplained.com/articles/fun-with-modular-arithmetic/ Modulo is used in different ways... For example you can say "Hey do something untel x % y == z"
Also
- 1%3 = 1
- 2 % 3 = 2
- 3 % 3 = 0 You can check if a number is devisible. There are a lot of practical uses of modulo https://en.wikipedia.org/wiki/Modulo_operation
john larson
16,594 Pointsjohn larson
16,594 PointsI didn't look at those posts yet but I will. I guess what I meant was: x%2 = 0 result is even. x%2 =1 result is odd. cause 1%2 = 1, at least in python calculations or at least in my python console.