+ 2

what is the meaning of "return not" in this code?

def is_even(x): if x == 0: return True else: return is_odd(x-1) def is_odd(x): return not is_even(x) print(is_odd(17)) print(is_even(23)) output is true false why this false is coming?

11th Apr 2018, 11:49 AM
Nimit Chhabra
Nimit Chhabra - avatar
3 ответов
+ 2
There is no "return not". There is only return. "not" is a boolean operator that returns the boolean inverse of the value. In your code , is_odd fun returns " not is_even". Whatever is_even fun returns,"not" will reverse that. If is_even returns True, not will change it into False.
13th Apr 2018, 1:07 AM
Shane Ko Naung
Shane Ko Naung - avatar
0
Because 23 is not even
11th Apr 2018, 11:55 AM
Hallox
Hallox - avatar
0
Here's the same code with some suitable output, to help you understand the recursion: https://code.sololearn.com/c3U3O9H1Q10e/?ref=app
11th Apr 2018, 1:19 PM
Emma