Python Forum

Full Version: Simple Code Understanding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.