If a return is hit then that result is returned and nothing else is processed. In the Codecademy solution return False is basically the default behaviour, and True only gets returned if the return in the if statement happens to be called.
Consider an if statement without returns:
a = 1
b = 1
if a == b:
print("These don't match!")
These don't match!
Your output will always include “These don’t match!” because it sits outside of the if statement, to ensure that this works correctly you would need to have an else in there instead
So then, in following with the second code solution that was suggested by Codecademy where the else statement was omitted and replaced simply with … return False … why not just do the same in your example …
a = 42
if a % 6 and a % 7:
print (‘Not divisible by 6 and 7’)
print (‘Divisible by 6 and 7’)
I am guessing it has something to do with the last print statement being in global realm (is that the correct term)? As in the last print statement will always be printed because it is outside of the function.
Second question, there’s that thing again in your elegant code where it’s assumed that the return statement will yield a True or False statement instead of another type of value. How does that work?
Because we don’t want the possibility for both print statements, only one in each case. The flow still comes to the last line. else is only read when the if is falsy. The example is not a function, just inline code.
It works as a comparison, which is boolean. A bool can only be one of, True or, False.