Python Forum
Simple Code Understanding - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Simple Code Understanding (/thread-8880.html)



Simple Code Understanding - jster2001 - Mar-11-2018

Hi,

I'm quite new to Python, looking to get a general understand how the below out puts what it does:

my_list = range(4,11)
for num in my_list:
    if (1==num%2):
       print (str(num))
Output (5, 7, 9)

Why is 1 (in the if statement) unable to be changed?

Any information would be great. Thank you Smile


RE: Simple Code Understanding - wavic - Mar-11-2018

nom % 2 - This operator is called modulo operator and it yields the remainder of the division.

1 == x checks if x is equal to 1.

The whole expression 1 == num % 2 checks if num is odd.