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 Python Basics (2015) Logic in Python Around and Around

Mariya Shklyar
Mariya Shklyar
1,022 Points

Loops 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.

I 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.

2 Answers

Hi 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

Thank you :-)

Michael Rathbun
Michael Rathbun
1,624 Points

Yes 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.

for num in [1, 2, 3, 4]:
    if num % 2 == 0:

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.

Tobias, 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

Sry 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

Ok, I see how it works now. returns 0 = even. returns 1 = odd. Did I get that right?

Maybe 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"