First off, when working always pay close attention to indentation, i.e. the number of spaces of each line with respect to the line immediately above. In your case, the script can be divided into two logical parts:
def isEven(num):
return num % 2 == 0
This function returns the value of the operation num % 2 == 0
. The operation num % 2
evaluates the remainder of the division between num
and 2
. It is easy to see by plugging numbers that it will always give 0 for even num
, and 1 for odd num
. Then, since the operator ==
evaluates whether the left hand side is logically equal to the right hand side of it, the statement num % 2 == 0
will simply give True
for even num
and False
for odd num
.
if isEven(3):
print("3 is even")
else:
print("3 is not even")
First, it is important to note that, since isEven
returns a value (a bool
), then wherever you call that function, you can substitute the value returned of that function in order to "simplify" things. Then, the statement if isEven(3)
can be rephrased as "is the value returned by isEven(3) true or false?" As you probably learned, if the statement if
is evaluated to False
and is accompanied by an else
, then the script will valuate what is inside the else
statement. In other words, this second piece of code will instruct the computer to print "3 is even" if 3 is even (i.e. if isEven(3)
returns True
, and "3 is not even" otherwise. Finally, since we know that 3 IS odd, 3%2 = 1
, and thus the program will simply print "3 is not even".